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:
- Remove the package from the
node_modulesdirectory. - Remove the package from the
dependencies,devDependencies, andpeerDependenciesobjects of thepackage.jsonfile. - Update the
package-lock.jsonfile 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 installcommand, npm will automatically run thenpm prunecommand 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
npmcommand to easily verify the content of the cache folder.
Summary
Here's a summary of what you've learned in this lesson:
- The
npm uninstallcommand is used to remove packages locally. - The
npm uninstall -gcommand is used to remove packages globally. - The
npm prunecommand is used to remove unused packages. - The
npm cache clean --forcecommand 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