npm install the exact package version specified in package.json

85,348

Solution 1

That behavior is really driven by the one specifying the versions in the package.json. If the version number looks like "1.0.0", without any other symbols, the exact version (1.0.0) should be installed.

So what you could do is simply modify the package.json and run a npm install then. Be sure to clear out the node_modules directory before you do that.

https://docs.npmjs.com/files/package.json#dependencies

Solution 2

By default npm installs packages using ^ which means any version in the same major range, you can switch this behaviour by using --save-exact

// npm
npm install --save --save-exact react

// yarn
yarn add --exact react

I created a blog post about this if anyone is looking for this in the future.

https://www.dalejefferson.com/articles/2018-02-04-how-to-save-exact-npm-package-versions/

Solution 3

You can also open package.json and change value for the package you want to remain exact. From "vue": "^2.6.10" to "vue": "2.6.10". Notice the lack of ^ sign in front of the version number.

Share:
85,348

Related videos on Youtube

suheb
Author by

suheb

Software developer with experience in Android and Web.

Updated on July 08, 2022

Comments

  • suheb
    suheb almost 2 years

    Currently, If I run npm install, it installs the updated version of already installed packages. How can I install the exact version as specified in the package.json file?

    • Sirko
      Sirko over 7 years
      how have you specified the version in the package.json? there is a modifier for fixed version.
    • suheb
      suheb over 7 years
      My bad, package.json had versions specified as ^version. I just assumed this how to versions. Will remove the ^ modifier. Thanks!
  • Victor Yarema
    Victor Yarema over 7 years
    Please note that there is still one issue with all subdependencies. Even if you specify strict versions for direct dependencies, the is no guarantee that those in turn will not trigger the installation of something new when it will be released.
  • davnicwil
    davnicwil over 5 years
    Also, for both npm and yarn -E is the short form of --save-exact / --exact
  • David Callanan
    David Callanan over 4 years
    I can't believe this is the only solution. It's very important for those creating packages to test with the lowest possible number. I'm considering creating a separate package-min.json file and using that as the package.json in CI
  • Timo
    Timo about 3 years
    Is exact version necessary or is the same major range ok
  • rmolinamir
    rmolinamir over 2 years
    Is there any way to make this behavior default across all installations with npm?