107

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?

2
  • 2
    how have you specified the version in the package.json? there is a modifier for fixed version.
    – Sirko
    Dec 6, 2016 at 19:41
  • 2
    My bad, package.json had versions specified as ^version. I just assumed this how to versions. Will remove the ^ modifier. Thanks!
    – suheb
    Dec 6, 2016 at 19:53

3 Answers 3

109

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/

4
  • 13
    Also, for both npm and yarn -E is the short form of --save-exact / --exact
    – davnicwil
    Jan 14, 2019 at 14:42
  • Is exact version necessary or is the same major range ok
    – Timo
    Mar 9, 2021 at 18:54
  • 1
    Is there any way to make this behavior default across all installations with npm?
    – rmolinamir
    Jan 26, 2022 at 18:49
  • 2
    yes, there is: npm config set save-exact=true or you can add save-exact=true to your .npmrc manually
    – TomRaaff
    Jul 15, 2022 at 11:48
64

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

2
  • 5
    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. Dec 28, 2016 at 17:12
  • 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 Aug 15, 2019 at 9:56
41

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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