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

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

I think youre 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 packages dependencies list.

See the docs: https://docs.npmjs.com/cli/prune

You could remove your node_modules/ folder and then reinstall the dependencies from package.json.

rm -rf node_modules/
npm install

This would erase all installed packages in the current folder and only install the dependencies from package.json. If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time.

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

Due to its folder nesting Windows can’t delete the folder as its name is too long. To solve this, install RimRaf:

npm install rimraf -g

rimraf node_modules

Leave a Reply

Your email address will not be published.