502

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.

Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
  • 2
    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 – Eric Wong Aug 16 '20 at 22:58
  • Fwiw, the new package manager pnpm keeps node_modules clean, only makes folders for your top level deps. Give it a try! – Andy May 19 '23 at 02:35

17 Answers17

509

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

David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • 2
    As far as I know, in new NPM version, all the dependencies are located at the root `node_modules` folder, and not as before, where each dependency had it's own dependencies install in their own `node_modules` folders..with countless copies of the same dependencies... so does `npm prune` takes this into consideration? because those deep-dependencies aren't written on the main `package.json` of your project.. prune must look recursively. – vsync Mar 05 '16 at 15:49
  • 4
    @vsync Duplication reduction is achieved with the command `npm dedupe` https://docs.npmjs.com/cli/dedupe . It tries to simplify the node tree by moving dependencies up the tree. – knaos Mar 14 '17 at 09:14
  • 2
    I've seen NPM prune fail to delete things usually screwing up when my corporate repository has something borked in it. Then I have to rm-rf. It would be nice if there was a "blow everythin away then reget everything all in one go" command – ggb667 Feb 20 '18 at 14:39
  • NPM 7 and Yarn 2+ will automatically prune on install, this command is not needed anymore normally – Eric Burel May 06 '22 at 11:07
  • 1
    `npm ci` seems to be a recently introduced command for this job. reference - https://stackoverflow.com/a/63081837/6908282 – Gangula Dec 15 '22 at 13:29
322

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.

adamduren
  • 3,857
  • 1
  • 14
  • 10
  • 7
    Yes, I know I can use this method but I am curious why there is still no more elegant solution for that. – Sergei Basharov Jan 14 '14 at 19:48
  • Are there really no npm command to delete node_modules other than deleting the folder? – Anders Dec 10 '14 at 15:22
  • 6
    On windows platform, it fails while deleting node_modules thru Explorer or command line (since the path to some modules are longer than 256). There should be npm command to do it in a cleaner way. – Ravi Kumar Aug 09 '15 at 05:39
  • @RaviKumar Try install rimraf first through npm. It handles deleting folders very well. – Sean Anderson Dec 31 '15 at 04:21
  • 26
    Or using `rmdir node_modules /s /q` on Windows. http://superuser.com/a/179661/440976 – Wédney Yuri Jan 30 '16 at 19:09
  • 11
    I think, this answer is missing a very important point: **if you are constrained by the traffic (i.e. you have the expensive per-megabyte-paid connection like mobile) and you have a lot of modules, this may cost you**, while the `prune` option does require you to have internet connection at all. – Ivan Kolmychek Jul 25 '16 at 13:16
  • 1
    `rm -rf node_modules && npm install` is slower but the only actual reliable way. `npm update && npm prune` will, e.g., [not remove packages referenced by `peerDependencies`](https://github.com/npm/npm/issues/10442). – binki Jul 11 '17 at 03:17
  • Remove-Item : A parameter cannot be found that matches parameter name 'rf'. – rsc05 Dec 09 '21 at 02:23
134

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
user3844078
  • 1,553
  • 2
  • 10
  • 12
61

From version 6.5.0 npm supports the command clean-install (ci) to hard refresh all the packages.

Please, see the references:

Giuseppe B
  • 1,194
  • 8
  • 7
  • 2
    This is exactly what I needed. When I update node, the first thing that happens is "looks like you you've changed your environment since running 'npm install'". Since I'm already in the console, it's easiest to just do clean-install rather than messing around deleting folders first etc. – Jarrod McGuire Oct 01 '19 at 14:03
  • This should be the accepted answer. `npm ci` is also great for continuous integration. It runs faster if you don't have a node_modules directory, so on our servers we backup the previous build by renaming node_modules to something else (for example, node_modules_backup. If a backup already exists, delete it first and then rename). We then run `npm install` as a hassle-free swift package update solution. – Nadav Oct 15 '21 at 03:37
  • thanks @Nadav, but to be fair the question was asked in 2014 and at the time there was no such a thing, so the author approved the best-at-the-time answer :) – Giuseppe B Jul 18 '22 at 09:55
46

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.

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
Ajay Kotnala
  • 985
  • 9
  • 15
  • 3
    Doesn't work, I get a "permission denied" error (using gitbash. – drake035 Mar 04 '17 at 12:23
  • 3
    open gitbash in administrator mode. i guess that will help. or change folder permission level settings – Ajay Kotnala Apr 24 '17 at 07:58
  • 3
    This would delete _**all**_ packages. The OP clearly says "I want to clean node\_modules folder so that only modules listed in package.json stay there". – Boaz Jul 04 '18 at 12:14
  • in that case my friend you can simply do "npm uninstall package_name " or --save as sufix that will delete its entry from package.json file as well. npm uninstall package_name (delete package) npm uninstall package_name --save (delete package and remove entry from dependencies in package json) npm uninstall package_name --save-dev (delete package and remove entry from devdependencies in package json) – Ajay Kotnala Aug 06 '18 at 09:09
31

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
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Ankit Parmar
  • 670
  • 10
  • 24
  • 1
    good answer, but installing a package to uninstall other ones is not a good idea though. `npm prune` is an ideal solution. –  Mar 11 '20 at 05:51
  • Yes, but if you want to remove the node_module folder from your project this is the best and easy way. – Ankit Parmar Mar 12 '20 at 19:00
16

Just in-case somebody needs it, here's something I've done recently to resolve this:

npm ci - If you want to clean everything and install all packages from scratch:

-It does a clean install: if the node_modules folder exists, npm deletes it and installs a fresh one.

-It checks for consistency: if package-lock.json doesn’t exist or if it doesn’t match the contents of package.json, npm stops with an error.

https://docs.npmjs.com/cli/v6/commands/npm-ci

npm-dedupe - If you want to clean-up the current node_modules directory without deleting and re-installing all the packages

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.

https://docs.npmjs.com/cli/v6/commands/npm-dedupe

parkourkarthik
  • 834
  • 1
  • 8
  • 20
14

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.

robertovg
  • 1,018
  • 11
  • 16
12

Have you tried npm prune?

it should uninstall everything not listed in your package file

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

7

I have added few lines inside package.json:

"scripts": {
  ...
  "clean": "rmdir /s /q node_modules",
  "reinstall": "npm run clean && npm install",
  "rebuild": "npm run clean && npm install && rmdir /s /q dist && 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

Ravi B
  • 1,574
  • 2
  • 14
  • 31
Amitesh Singh
  • 162
  • 2
  • 7
6

You can also use npx in combination with rimraf to remove all node modules with one command, so you don't have to install rimraf first.

So go to the path where your node_modules folder is located by using cmd (in case you are not there already) and run the following command

npx rimraf node_modules
Johannes
  • 69
  • 1
  • 4
4

I recently upgraded Angular from 14 to 15, and found that I need to do:

npm cache clean --force
npm cache verify
rm -rf node_modules/
npm install

If I don't do cache clean, the install will success locally, but failed when run deploy on server. This is because local install will use local cached package to install. be aware. :)

Jack Luo
  • 333
  • 3
  • 5
2

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

Osify
  • 2,253
  • 25
  • 42
0

Use following command instead of npm install

npm ci
Jasmin Mistry
  • 1,459
  • 1
  • 18
  • 23
0

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
hamid_reza hobab
  • 925
  • 9
  • 21
0

If you are using YARN in your project please add below line to your pacjage.json script and run yarn clean to execute on project terminal

"clean": "rm -rf yarn.lock node_modules ios/Podfile.lock && yarn install && cd ios && pod install && pod update && cd .."
Sagar
  • 5,273
  • 4
  • 37
  • 50
-1

Simply should run this rm -rf node_modules/ npm install