Open In App

Remove NPM Package

NPM, which stands for Node Package Manager, is the default package manager of Node.js. Developed by Isaac Z. Schlueter, NPM is written in JavaScript and was initially released on the 12th of January, 2010.

Removing npm packages is a common task in Node.js development, whether you’re cleaning up dependencies, troubleshooting issues, or streamlining your project’s footprint. In this guide, we’ll walk through the various methods to uninstall npm packages from your project.



Removing Package using npm

To remove a package using the npm package manager, we can employ the command npm uninstall. The command npm uninstall is used to uninstall a package whether it is local or global.



npm uninstall package_name [package_names]

Here, package_name: The name of packages to uninstall.

Removing Packages from package.json

When you uninstall a package using npm uninstall, npm automatically removes the package from your package.json file’s dependencies or devDependencies section. If you want to remove a package from your project’s dependencies without uninstalling it, you can use the –save or –save-dev flag:

npm uninstall --save <package-name>  # Removes from dependencies
npm uninstall --save-dev <package-name> # Removes from devDependencies

Removing Multiple Packages

You can uninstall multiple packages simultaneously by providing their names separated by spaces:

npm uninstall package1 package2 package3

Removing Globally Installed Packages

The npm uninstall command can also be used to remove a package from the global environment. The -g option allows this to take place. In the following command we remove the mongoose package from the global environment:

npm uninstall -g <package-name>

Example:

npm uninstall -g mongoose

Output:

Uninstall mongoose package

Uninstall Specific Version Package

The npm uninstall command can be used to remove only a specific version of a package. The following command uninstalls the version 4.18 of express package. The number after the @ symbol specifies the version.

npm uninstall express@4.18

Output:

Uninstall express version 4.18

Uninstalling all Unused Packages

The npm package manager provides the command npm prune which can be used to all the unused packages. We can use the npm prune command as follows:

npm prune

Output:

Remove all the unused libraries

Conclusion

Uninstalling npm packages is a straightforward process using the npm CLI. Whether you’re removing individual packages, cleaning up unused dependencies, or managing global installations, npm provides flexible options to streamline your Node.js projects. By regularly reviewing and removing unnecessary packages, you can keep your projects lean and maintainable.

Article Tags :