570

Is there a simple way to reinstall all packages that my app depends on (i.e. they are in my apps node_modules folder)?

2
  • 8
    npm rebuild might also help some issues with native dependencies Commented Jun 14, 2016 at 16:24
  • 1
    In case it helps others: If you're just trying to reinstall one package, npm update <packageName> works great
    – rinogo
    Commented Oct 14, 2016 at 22:24

10 Answers 10

893

The easiest way that I can see is delete node_modules folder and execute npm install.

15
  • 48
    Agreed; assuming you've created a package.json file for your app.
    – JohnnyHK
    Commented Oct 12, 2012 at 20:24
  • 17
    Beware 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
    Commented Oct 12, 2012 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
    Commented Oct 12, 2012 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
    Commented Oct 18, 2012 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. Commented Oct 18, 2012 at 23:12
143

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.

11
  • 88
    But 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
    Commented Oct 12, 2012 at 21:01
  • 3
    If 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 <package_name> to uninstall specific package or execute npm uninstall to uninstall all packages.
    – himanshu
    Commented Oct 12, 2012 at 21:09
  • 1
    I tried doing just npm uninstall without specifying a package but that throws an error.
    – trusktr
    Commented Oct 12, 2012 at 21:42
  • 1
    The link you provide is dead, can you please supply a new one? (I found this but I'm not sure it's acceptable to change to it)
    – Motti
    Commented Sep 13, 2016 at 8:41
  • 1
    No, 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
    Commented Sep 15, 2021 at 14:12
117

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 and npm ci are:

  • The project must have an existing package-lock.json or npm-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 before npm ci begins its install.
  • It will never write to package.json or any of the package-locks: installs are essentially frozen.
4
  • 1
    This answer adds some info regarding npm ci: stackoverflow.com/a/64014814/10788155
    – Ictus
    Commented Aug 21, 2022 at 10:34
  • 2
    ci does not work for global packages that are to be installed with -g option Commented Mar 17, 2023 at 5:58
  • @RajaNagendraKumar The question is about reinstalling app dependencies, not global packages. Commented Mar 22, 2023 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
    Commented Apr 9, 2023 at 21:18
107

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.

84

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

1
  • 2
    My experience is that this doesn't work. The docs say that it does, but it simply doesn't.
    – Halcyon
    Commented Aug 18, 2021 at 8:57
42

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

0
20

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+)

1
  • 1
    @g00glen00b I've edited so that it now does include useful information without hyperlinks
    – Rob
    Commented Jul 27, 2017 at 5:55
9

Delete node_module and re-install again by command

rm -rf node_modules && npm i
2

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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.