113

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?

manonthemat
  • 6,101
  • 1
  • 24
  • 49
suheb
  • 1,509
  • 2
  • 13
  • 19
  • 2
    how have you specified the version in the `package.json`? there is a modifier for fixed version. – Sirko Dec 06 '16 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 06 '16 at 19:53

4 Answers4

119

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/

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dale Jefferson
  • 1,730
  • 1
  • 14
  • 7
68

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

manonthemat
  • 6,101
  • 1
  • 24
  • 49
  • 6
    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. – Victor Yarema Dec 28 '16 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 – David Callanan Aug 15 '19 at 09:56
42

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.

Vladimir Jovanović
  • 3,288
  • 1
  • 20
  • 27
0

another best way which works for me

npm i --legacy-peer-deps
Shoaib Dev
  • 71
  • 6
  • 4
    Please include in the answer why this works and what it does differently to other answers. – phuzi May 23 '23 at 09:48