ChatGPT解决这个技术问题 Extra ChatGPT

npm command to uninstall or prune unused packages in Node.js

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.

Unused by what? Do you mean to remove folders from node_modules when they're removed from the respective package.json?
exactly, mhm npm ll already gives a good hint which are the candidates.

D
Darkhogg

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.


If 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.
Let me give an example. I remove karma from my package.json, but leave bower. When I run npm prune, I expect all of karma, including its own node_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's package.json. Are those removed or not?
No, they are not. Note that they're not in your own node_modules, but inside node_modules/bower/node_modules, "protected" by node_modules/bower/package.json. Dependencies of your package and that of your package's dependencies are not mixed.
and delete your shrinkwrap before npm install, should have been in above instructions.
I 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 run npm prune and everything ends right.
P
Pyrce

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.


It 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.
Rebuilding node_modules also verifies the package.json file describes a reproducible dependency graph. Removing and re-installing your node_modules is basically a deploy test.
npm prune didn't help one iota, but this did. My problem was a broken symlink.
Under 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.
a handy one liner is rm -rf node_modules && npm install punch it in walk away come back. Life is good.
I
Igor Litvinovich

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 c@1.0.x
|    `-- c@1.0.3
`-- d <-- depends on c@~1.0.9
     `-- c@1.0.10

In this case, npm-dedupe will transform the tree to:

 a
 +-- b
 +-- d
 `-- c@1.0.10

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!
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.
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.