Sergei Basharov Sergei Basharov - 10 months ago 92
Node.js Question

How to clean node_modules folder of packages that are not in package.json?

Assume I install project packages with

npm install
that looks into
for modules to be installed. After a while I see that I don't need some specific module and remove its dependency from
. Then I do it more times because some modules are not needed anymore and others are replaced with alternatives.

Now I want to clean node_modules folder so that only modules listed in
stay there and the rest must go, something like
npm clean
. I know I can remove them manually but would like to have some nice ready to use sugar functionality for that.

Answer Source

I think you're looking for npm prune

npm prune [<name> [<name ...]]

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.

See the docs: