How do you reinstall an app's dependencies using npm?

node.jsNpm

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

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.

Solution 3 - node.js

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.

Solution 4 - node.js

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

Solution 5 - node.js

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.

Solution 6 - node.js

Solution 7 - node.js

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

Solution 8 - node.js

Delete node_module and re-install again by command

rm -rf node_modules && npm i

Solution 9 - node.js

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.

Solution 10 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontrusktrView Question on Stackoverflow
Solution 1 - node.jsVadim BaryshevView Answer on Stackoverflow
Solution 2 - node.jshimanshuView Answer on Stackoverflow
Solution 3 - node.js0x1ad2View Answer on Stackoverflow
Solution 4 - node.jsItsik AvidanView Answer on Stackoverflow
Solution 5 - node.jsJoeriView Answer on Stackoverflow
Solution 6 - node.jsJaa HView Answer on Stackoverflow
Solution 7 - node.jsdeksdenView Answer on Stackoverflow
Solution 8 - node.jsSurender KumarView Answer on Stackoverflow
Solution 9 - node.jsmichal.jakubeczyView Answer on Stackoverflow
Solution 10 - node.jsChukwuEmekaView Answer on Stackoverflow