Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force npm to reinstall a single package, even if the version number is the same?

Tags:

npm

In my Node.js project, I have a dependency on another local project. Oftentimes, I need to make a small change to the dependency and see how it affects my main project. In order to do this, I have to reinstall my dependency using npm.

I can use npm update to try to update my dependency, but this seems like it will only work if the version number has changed on the dependency. I don't want to have to change the version number on my dependency every time I change a line of code or two to make an experimental change in development.

I can rm -rf node_modules/; npm install to ensure that I get the latest versions of all of my dependencies. Downloading all of my non-local dependencies takes several minutes, breaking up my train of thought.

Is there a way to force npm to reinstall a single dependency, even if that dependency's version number hasn't changed?

like image 436
Kevin Avatar asked Dec 13 '16 22:12

Kevin


People also ask

How do I install a specific version of a package?

Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.

How do I update npm packages to a specific version?

To update a specific package, we need to run the npm update command followed by the package name. Sometimes, you want to update a package to the specific version in such cases you need to use npm install command by specifying a version number after the package name.


2 Answers

When you run npm install, it will install any missing dependencies, so you can combine it with an uninstall like this:

npm uninstall some_module; npm install

With npm 5, uninstalled modules are removed from the package.json, so you should use:

npm uninstall some_module; npm install some_module
like image 160
cartant Avatar answered Sep 21 '22 17:09

cartant


On npm v 6.14:

npm install module_name --force --no-save

You get a message stating:

npm WARN using --force I sure hope you know what you are doing.

And then it proceeds to uninstall and reinstall the package.

Note: if you don't specify the --no-save option, npm updates the package version on package.json to the highest version that is compatible with the existing SemVer rule.
If you do not want npm to update the package's version on package.json, keep the --no-save option.

like image 37
Cláudio Silva Avatar answered Sep 18 '22 17:09

Cláudio Silva