The Node Package Manager (NPM) is the default package manager for Node.js. NPM provides us with many commands to install, update and manage our dependencies or packages. As it allows us to install node modules from the npm registry, it also allows us to uninstall modules.
Simply put, packages or dependencies are those whose functions and functionalities can be used in our project by importing them using the require() statement.
In this article, we will learn how we can uninstall npm packages or dependencies from our Node.js project.
Uninstalling local npm packages
Local packages are the npm modules that are installed in the directory where you run npm install <package_name>, and are put in the node_modules folder under this directory. The name of such packages is also referenced as keys in different dependencies sections in the package.json file of the repository.
To uninstall a local package we can use the uninstall command from npm. Doing so will remove everything npm installed on its behalf, the package from node_modules, and its reference in any dependencies, devDependencies, optionalDependencies, and peerDependencies objects in package.json.
npm uinstall <package_name>
If your package has a scope you may also add the scope of the package before package_name
npm uinstall <@scope/package_name>
To explain the npm uninstall command, I have taken an existing Node.js project where you can see nodemon is installed as a dev dependency.
Run the following commands in your terminal to uninstall and remove nodemon from the project
npm uninstall nodemon
You can find nodemon has been completely removed from the local project. Secondly, the devDependencies section has also been removed because there are no devDependencies in the project.
Removing local packages from node_modules but not from package.json
Additionally, we can also remove everything that belongs to the local package but still keep its reference in the package.json file using a –no-save flag.
npm uninstall --no-save <package_name>
In this example, you can see we have express installed in our Node.js project and our node_modules folder has an express folder along with other dependencies of the express package.
Now let’s try uninstalling the package with the –no-save flag
npm uninstall --no-save express
We can see that everything that was installed with express including express has been removed from our node_modules however, a reference to express has not been lost in our package.json.
Uninstalling global npm packages
Unlike local packages which can be found in the node_modules of our project, global packages are installed at some place in our system depending on our Node installation and operating system irrespective of where we run npm install-g <package_name>.
To uninstall a global package from the system we need to use the -g flag with the uninstall command.
npm uninstall -g <package_name>
Open any terminal/PowerShell and run the below command.
npm list -g
This will give you the list of globally installed npm modules. In my system, I have the following packages installed.
+-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] +-- [email protected] `-- [email protected]
To follow along you may install any package such as React globally using
npm install -g react
Now, we will uninstall the globally installed react package with the following command
npm uninstall -g react
Once the uninstallation is over, again run npm list -g to see the new list of globally installed packages and as expected we won’t find our React package in the list.
NPM is not just the default package manager for Node.js but is extensively feature-rich when it comes to managing packages. In this article, we covered some basics of different installations of npm modules and then discussed how we can uninstall them from our local Node project and also globally from the system.
Wondering what to read next? Learn about the –save option for npm install.