Is there a way to simply uninstall all unused (undeclared) dependencies from a Node.js project (ones that are no longer defined in my package.json
.) When I update my application I like to have the unreferenced packages removed automatically.
4 Answers
Note: Recent npm
versions do this automatically when running npm install
if package-locks are enabled, so this is not necessary except for removing development packages with the --production
flag.
Run npm prune
to remove modules not listed in package.json
.
From npm help prune
:
This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.
Extraneous packages are packages that are not listed on the parent package's dependencies list.
If the
--production
flag is specified, this command will remove the packages specified in your devDependencies.
-
5If I read that correctly, this would remove all sub-dependencies, since they're not listed in
package.json
. Is that right? So, the next update or install would have to reinstall them.– nshew13Commented Apr 18, 2014 at 14:14 -
2Let me give an example. I remove karma from my
package.json
, but leave bower. When I runnpm prune
, I expect all of karma, including its ownnode_modules
folder containing its dependencies, to be removed. What about bower's dependencies (bower-json, bower-logger, chmodr, fstream, glob, et al.). Technically, those aren't listed in my project'spackage.json
. Are those removed or not?– nshew13Commented Apr 18, 2014 at 14:27 -
4No, they are not. Note that they're not in your own
node_modules
, but insidenode_modules/bower/node_modules
, "protected" bynode_modules/bower/package.json
. Dependencies of your package and that of your package's dependencies are not mixed.– DarkhoggCommented Apr 18, 2014 at 14:30 -
2and delete your shrinkwrap before npm install, should have been in above instructions.– Andy RayCommented May 4, 2015 at 22:30
-
2I use the
depcheck
package installed globally to check what packages are not being used. Then I go to the package-json and remove the unused packages. After those two steps, I runnpm prune
and everything ends right. Commented Feb 8, 2018 at 4:33
If you're not worried about a couple minutes time to do so, a solution would be to rm -rf node_modules
and npm install
again to rebuild the local modules.
-
134It would be nice if people stopped downvoting this without comment.. it's a valid strategy to resetting a node project dependencies as an alternative to the accepted answer. If you damaged a node_modules sub-directory contents (easy to do with sym-linked dependencies) or if you've had additional changes like node or npm version bumps prune will not properly cleanup the node_modules folder but this answer will.– PyrceCommented Jan 12, 2016 at 19:06
-
63Rebuilding
node_modules
also verifies thepackage.json
file describes a reproducible dependency graph. Removing and re-installing yournode_modules
is basically a deploy test. Commented Jan 24, 2016 at 18:26 -
14
npm prune
didn't help one iota, but this did. My problem was a broken symlink. Commented Apr 22, 2016 at 0:34 -
11Under many non-ideal circumstances that's currently infeasible with npm. Also the question definitely did not specify some constraint on repeated work or additional fetching, just how to achieve the end goal. This answer satisfies the question as stated, despite what others may want beyond that goal.– PyrceCommented May 4, 2016 at 18:41
-
9a handy one liner is
rm -rf node_modules && npm install
punch it in walk away come back. Life is good. Commented Sep 11, 2017 at 23:18
You can use npm-prune to remove extraneous packages.
npm prune [[<@scope>/]<pkg>...] [--production] [--dry-run] [--json]
This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.
Extraneous packages are packages that are not listed on the parent package's dependencies list.
If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies. Setting --no-production will negate NODE_ENV being set to production.
If the --dry-run flag is used then no changes will actually be made.
If the --json flag is used then the changes npm prune made (or would have made with --dry-run) are printed as a JSON object.
In normal operation with package-locks enabled, extraneous modules are pruned automatically when modules are installed and you'll only need this command with the --production flag.
If you've disabled package-locks then extraneous modules will not be removed and it's up to you to run npm prune from time-to-time to remove them.
Use npm-dedupe to reduce duplication
npm dedupe
npm ddp
Searches the local package tree and attempts to simplify the overall structure by moving dependencies further up the tree, where they can be more effectively shared by multiple dependent packages.
For example, consider this dependency graph:
a
+-- b <-- depends on [email protected]
| `-- [email protected]
`-- d <-- depends on c@~1.0.9
`-- [email protected]
In this case, npm-dedupe will transform the tree to:
a
+-- b
+-- d
`-- [email protected]
Because of the hierarchical nature of node's module lookup, b and d will both get their dependency met by the single c package at the root level of the tree.
The deduplication algorithm walks the tree, moving each dependency as far up in the tree as possible, even if duplicates are not found. This will result in both a flat and deduplicated tree.
-
I have MORE items in my node_modules folder after running npm dedupe. Sigh!– NevilleCommented Nov 8, 2019 at 15:22
-
does remove and uninstall mean the same thing when it comes to using prune? I ran a prune instance with a
--production
flag. But my package.json file never udpated.– klewisCommented Feb 18, 2021 at 16:18 -
prune removes those files that present in the node_modules folder but not listed as any package's dependency list. If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies. Commented Feb 19, 2021 at 17:28
You can run npx depcheck
to get the list of dependencies and devDependencies that are not being used in the project.
Then you can run npm uninstall pkg1 pkg2
and so on to remove the dependencies.
Or you can also remove the dependencies from package.json and reinstall the packages after deleting node_modules and package-lock.json
node_modules
when they're removed from the respectivepackage.json
?