Is there a simple way to reinstall all packages that my app depends on (i.e. they are in my apps node_modules folder)?
10 Answers
The easiest way that I can see is delete node_modules
folder and execute npm install
.

- 25,689
- 4
- 56
- 48
-
46
-
17Beware that running `npm install` after removing the `node_modules` directory can result different versions of dependencies defined in `package.json` being pulled down. If you require very specific versions of dependencies for your app, be careful and look into `npm shrinkwrap` or checking in your `node_modules` directory to source control. – smithclay Oct 12 '12 at 21:18
-
16@smithclay I've defined the explicit version of the packages in my app's package.json, so that should be fine, right? – trusktr Oct 12 '12 at 21:35
-
1@vadim I was doing some testing on the behavior of `npm install` and it always reinstalls dependencies and upgrades packages to latest versions as permitted by package.json. So removing `node_modules` folders is not needed unless you think its corrupted. Is there a reason you think it should be removed? However, if the intent is to remove any packages that are extraneous then you could execute `npm prune` instead – himanshu Oct 18 '12 at 19:27
-
3@himanshu you are right `npm install` upgrades all modules to package.json state. But the question is _how to reinstall all packages_. So they may be corrupted or may need to rebuild the binary parts after upgrade node.js verison. – Vadim Baryshev Oct 18 '12 at 23:12
-
@trusktr not necessarily. locking down specific versions will ensure *your* deps don't change, but you dependencies' dependencies could still change – jasonkarns Jan 30 '14 at 14:41
-
@jasonkarns This is if I remove my node_modules directory and run `npm install` right? So nothing will happen if my dependency numbers haven't changed and I try upgrading (not having removed `node_modules`)? Or will it?.. On another note, this could be a pain for modules in npmjs.org that rely on latest dependencies (no version numbers). Each time the user installs my package it could pull in different versions of the package's dependencies, even if he pulls a specific version of my package? If so, there's something inherently wrong with that. – trusktr Feb 01 '14 at 19:09
-
@jasonkarns It says here that name and version are required for npmjs packages: https://npmjs.org/doc/json.html. I haven't tested it, but I'm going to assume that not requiring both a name and version would make things very hectic. I'll try it next time I get to developing my Node.js stuff. – trusktr Feb 01 '14 at 19:25
-
@trusktr what do you mean "upgrading without deleting node_modules"? my original comment was that, yes, transitive dependencies can change without your own package.json changing. (if node_modules is removed and then run `npm i`) – jasonkarns Feb 06 '14 at 21:16
-
@jasonkarns I totally agree with what you said if you make a project on your hard drive adhoc, but it's not true if your package is going in npmjs.org. npmjs.org packages require version numbers in package.json. That's all I meant. :) – trusktr Feb 07 '14 at 04:36
-
@trusktr I've never once suggested omitting version numbers in package.json. Transitive dependencies can change even with package versions specified. Say I depend on module A@0.1.0. Module A depends on module B using ~0.4.0 (or 0.4.x or >=0.4.0). In this case, doing `npm update` (or `npm install` after removing node_modules) will get the latest version of module B that satisfies module A's package.json. So you could end up with B@0.4.9. Only way to lock down transitive deps is with `npm shrinkwrap`. – jasonkarns Feb 07 '14 at 04:46
-
@jasonkarns Oh, so we are allowed to put things like `0.4.x` or `>=0.4.0`in a package.json of a package that we put on npmjs.org? – trusktr Feb 08 '14 at 00:59
-
@jasonkarns Oh, in the "dependencies" section of a package's package.json. That is quite nasty! – trusktr Feb 08 '14 at 05:57
-
I updated _node.js_ & _npm_ from an outdated version after already using `npm install` on a project. The following did not work: `npm install` `npm update` `npm rebuild` But what did work is what @VadimBaryshev recommended: **1.)** delete `node_modules` folder **2.)** `npm install` – pfuri Sep 13 '16 at 06:32
-
I love workflows that the accepted process is "Delete the world then rebuild from scratch". Of course if some jackass unpublishes his packages when you do this... – Ray Nov 25 '16 at 21:47
The right way is to execute npm update
. It's a really powerful command, it updates the missing packages and also checks if a newer version of package already installed can be used.
Read Intro to NPM to understand what you can do with npm.

- 219
- 1
- 8

- 2,087
- 1
- 13
- 13
-
87But if there is no newer package then this won't do anything, right? I'm trying to *force* reinstall even if the package is already at latest version. – trusktr Oct 12 '12 at 21:01
-
3If there are no missing packages, then it will check if it can use latest versions of packages (including dependencies of the packages) listed in package.json . So, in essence it will upgrade packages if possible. But it will not reinstall. If you want to do it using commands you can execute `npm uninstall
` to uninstall specific package or execute `npm uninstall` to uninstall all packages. – himanshu Oct 12 '12 at 21:09 -
1I tried doing just `npm uninstall` without specifying a package but that throws an error. – trusktr Oct 12 '12 at 21:42
-
Do you have package.json in the folder directory where you executed this? If you are uninstall global package then you will need to add -g – himanshu Oct 12 '12 at 21:44
-
Yeah, package.json is there. It just doesn't work. It says `npm WARN uninstall not installed in /home/trusktr/src/myApp/nodeapp/node_modules:`. – trusktr Oct 13 '12 at 02:36
-
Are you executing this inside node_modules folder? if so can you try one level up? – himanshu Oct 13 '12 at 09:58
-
Yeah, I tried in the parent of the `node_modules` folder. No luck. I'm using Node 0.8.11. – trusktr Oct 13 '12 at 23:34
-
This also works for modules installed with "npm install path/to/some/lib" if the package.json version is changed. – Anna B May 28 '15 at 07:38
-
1The link you provide is dead, can you please supply a new one? (I found [this](https://howtonode.org/introduction-to-npm) but I'm not sure it's acceptable to change to it) – Motti Sep 13 '16 at 08:41
-
1No, that's not the right way and it's not an answer to the question. The question is how to reinstall and not how to update to latest version. I need to downgrade. – KulaGGin Sep 15 '21 at 14:12
-
For anyone trying to update a git url dependency, open the package.json, copy the name and do "npm uninstall [NAME]", and then once uninstalled, do CTRL + Z and CTRL + S to bring back(change the hash if desired) the dependency in the package.json file and then do "npm install". – Damien Golding Feb 11 '22 at 14:27
You can do this with one simple command:
npm ci
Here's an excerpt from npm ci
documentation:
In short, the main differences between using
npm install
andnpm ci
are:
- The project must have an existing
package-lock.json
ornpm-shrinkwrap.json
.- If dependencies in the package lock do not match those in
package.json
,npm ci
will exit with an error, instead of updating the package lock.npm ci
can only install entire projects at a time: individual dependencies cannot be added with this command.- If a
node_modules
is already present, it will be automatically removed beforenpm ci
begins its install.- It will never write to
package.json
or any of the package-locks: installs are essentially frozen.

- 7,797
- 3
- 48
- 67

- 1,168
- 1
- 5
- 9
-
This answer adds some info regarding `npm ci`: https://stackoverflow.com/a/64014814/10788155 – Ictus Aug 21 '22 at 10:34
-
1ci does not work for global packages that are to be installed with -g option – Raja Nagendra Kumar Mar 17 '23 at 05:58
-
@RajaNagendraKumar The question is about reinstalling app dependencies, not global packages. – Whitespacecode Mar 22 '23 at 16:24
-
In short: do ```npm ci``` if you want to reinstall your node_modules, but don't want to have your package-lock.json updated – Niki Herl Apr 09 '23 at 21:18
Most of the time I use the following command to achieve a complete reinstall of all the node modules (be sure you are in the project folder).
rm -rf node_modules && npm install
You can also run npm cache clean
after removing the node_modules
folder to be sure there aren't any cached dependencies.

- 8,014
- 9
- 35
- 48
npm
updated the CLI command for install
and added the --force
flag.
npm install --force
The --force
(or -f
) argument will force npm
to fetch remote resources even if a local copy exists on disk.
See npm install

- 4,660
- 5
- 27
- 40

- 1,681
- 1
- 12
- 7
-
2My experience is that this doesn't work. The docs say that it does, but it simply doesn't. – Halcyon Aug 18 '21 at 08:57
As of npm cli v6.5.0 you can use the backronym:
npm clean-install
Sources:
https://github.com/npm/cli/releases/tag/v6.5.0 https://github.com/npm/cli/commit/fc1a8d185fc678cdf3784d9df9eef9094e0b2dec

- 2,308
- 2
- 15
- 21
You can use the reinstall module found in npm.
After installing it, you can use the following command:
reinstall
The only difference with manually removing node_modules
folder and making npm install
is that this command automatically clear npm's cache. So, you can get three steps in one command.
upd: npx reinstall
is a way to run this command without globally installing package (only for npm5+)

- 774
- 6
- 13
-
1@g00glen00b I've edited so that it now does include useful information without hyperlinks – Rob Jul 27 '17 at 05:55
Delete node_module and re-install again by command
rm -rf node_modules && npm i

- 1,152
- 16
- 17
For Windows you can use
(if exist node_modules rmdir node_modules /q /s) && npm install
which removes node_modules
directory and performs npm install
then. Removal before install assures that all packages are reinstalled.

- 8,221
- 1
- 59
- 63
Follow this step to re install node modules and update them
works even if node_modules folder does not exist. now execute the following command synchronously. you can also use "npm update" but I think this'd preferred way
npm outdated // not necessary to run this command, but this will show outdated dependencies
npm install -g npm-check-updates // to install the "ncu" package
ncu -u --packageFile=package.json // to update dependencies version in package.json...don't run this command if you don't need to update the version
npm install: will install dependencies in your package.json file.
if you're okay with the version of your dependencies in your package.json file, no need to follow those steps just run
npm install

- 6,687
- 5
- 44
- 67