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

## The Question :

368 people think this question is useful

Assume I install project packages with npm install that looks into package.json for modules to be installed. After a while I see that I don’t need some specific module and remove its dependency from package.json. Then I remove some other modules from package.json because they are not needed anymore and others are replaced with alternatives.

Now I want to clean node_modules folder so that only modules listed in package.json 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.

• One thing all answers below and OP did not mention is: Make sure you have package lock before deleting node_modules, otherwise npm will re-evaluate the package.json and gives you the latest package versions, which with package that does not follow semver properly can become a nightmare

433 people think this answer is useful

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: https://docs.npmjs.com/cli/prune

232 people think this answer is useful

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.

113 people think this answer is useful

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



32 people think this answer is useful

simple just run

rm -r node_modules



in fact, you can delete any folder with this.

like rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete.

just open the gitbash move to root of the folder and run this command

Hope this will help.

25 people think this answer is useful

First globally install rimraf

npm install rimraf -g



go to the path using cmd where your node_modules folder and apply below command

rimraf node_modules



21 people think this answer is useful

from version 6.5.0 npm supports the command clean-install to hard refresh all the packages

10 people think this answer is useful

Have you tried npm prune?

it should uninstall everything not listed in your package file

https://npmjs.org/doc/cli/npm-prune.html

3 people think this answer is useful

I have added few lines inside package.json:

"scripts": {
...
"clean": "rmdir /s /q node_modules",
"reinstall": "npm run clean &amp;&amp; npm install",
"rebuild": "npm run clean &amp;&amp; npm install &amp;&amp; rmdir /s /q dist &amp;&amp; npm run build --prod",
...
}



If you want to clean only you can use this rimraf node_modules or rm -rf node_modules.

It works fine

2 people think this answer is useful

For Windows User, alternative solution to remove such folder listed here: http://ask.osify.com/qa/567

Among them, a free tool: Long Path Fixer is good to try: http://corz.org/windows/software/accessories/Long-Path-Fixer-for-Windows.php

2 people think this answer is useful

The best article I found about it is this one: https://trilon.io/blog/how-to-delete-all-nodemodules-recursively

All from the console and easy to execute from any folder point.

But as a summary of the article, this command to find the size for each node_module folder found in different projects.

find . -name "node_modules" -type d -prune -print | xargs du -chs



And to actually remove them:

find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;



The article contains also instructions for windows shell.

0 people think this answer is useful

Remove/Edit the packages that are not needed in package-lock.json (package names will be written under dependencies & devDependencies) and then

npm install

0 people think this answer is useful

rimraf is an package for simulate linux command [rm -rf] in windows. which is useful for cross platform support. for install its CLI:

npm install rimraf -g



npm ci