In this short article, we will learn how to uninstall npm packages locally or globally from our node projects.
The npm packages are installed in two ways : locally and globally.
Anytime we install an npm module using npm install < package-name >
, it installs the package in our node_modules folder and creates a reference in the package.json file.
Most packages are installed locally which means it is only available for the project in which they are installed.
The globally installed npm packages are available to be used by any project.
Let’s see how we can uninstall npm packages from our project.
Uninstall locally installed npm modules
To uninstall a node package that is installed locally, we have to run:
npm unistall <package-name>
from the root directory of the project. It will remove all the files related to the module from the node_modules folder and also remove its reference from the package.json file of the current project.
Optional flags provided by npm uninstall are :
-save or -S : It will remove packages from dependencies.
npm unistall < package-name > -S
-save-dev or -D : It will remove the npm package from the devDependencies
npm unistall < package-name > -D
-save-optional or -O : This flag will remove the npm package from optionalDependencies.
npm unistall <package-name> -O
It is important to note that these are optional flags. And most of the time, just using npm uninstall
with the package name does our job.
Uninstall globally installed npm modules
To uninstall any globally installed npm modules we have to use the --global
or -g
flag with the npm uninstall command.
Let’s say we need to uninstall the nodemon package completely from our computer. To do so we will run:
npm uninstall nodemon -g
or
npm uninstall nodemon --global
This will remove nodemon package and all the files related to the package from our computer completely.
Related Articles: