Remove Installed npm Packages

4 min read·Jan 1, 2025

Remove local packages

To completely remove unneeded packages from a project, you can use the npm uninstall command:

$ npm uninstall package ...

Where package ... is a list of package names.

When executed, it will:

  1. Remove the package from the node_modules directory.
  2. Remove the package from the dependencies, devDependencies, and peerDependencies objects of the package.json file.
  3. Update the package-lock.json file to reflect those changes in the dependency tree of the project.

Remove global packages

To uninstall a global package, you can use the npm uninstall command with the -g flag.

$ npm uninstall -g <package ...>

This will remove the package from the global npm installation directory.

Clean up unused dependencies

To remove any unused or unreferenced dependencies from the node_modules directory, to free up disk space and improve the performance of your application, you can use the npm prune command:

$ npm prune

This is particularly useful if you have recently removed a dependency from the package.json file, if you have multiple versions of a package installed, or if you have recently upgraded a package.

Note: When running the npm install command, npm will automatically run the npm prune command in the background.

Clean up the cache

When installing a package for the first time, npm will download the package in the node_modules folder of the project and automatically add a local copy of this package into the cache folder.

In the future, when reinstalling the same package, npm will reuse this local copy to speed up the installation process instead of downloading it again from the registry.

On Unix-like operating systems, the cache folder is located in the ~/.npm directory.

To delete all data out of the cache folder, you can use the npm cache clean command:

$ npm cache clean --force

And verify that the cache has been successfully cleared, using the npm cache verify command:

$ npm cache verify

Notes:

  • As of npm version 5, all data that passes through the cache is fully verified and automatically re-fetched in case of corruption. For this reason, it should never be necessary to clear the cache for any reason other than reclaiming disk space or reinstalling libraries free of cache.

  • There is, at the moment, no available npm command to easily verify the content of the cache folder.

Summary

Here's a summary of what you've learned in this lesson:

  • The npm uninstall command is used to remove packages locally.
  • The npm uninstall -g command is used to remove packages globally.
  • The npm prune command is used to remove unused packages.
  • The npm cache clean --force command is used to clean the cache folder.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Remove Installed Packages in npm | Backend Brewery