'why npm install does not respect version in package.json

Nodejs version - 14.17.3
npm version - 6.14.13

I am confused over the difference in package versions -
the one I have declared in package.json is different than the one that is being installed by npm install command.

package.json -

{
  "name": "training-project-metadata",
  "version": "1.0.0",
  "description": "",
  "main": "module1.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "tildify": "^2.0.0"
  },
  "bundleDependencies": [
    "tildify"
  ]
}

If I run npm install command, this is the package-lock.json that is being created -

{
  "name": "training-project-metadata",
  "version": "1.0.0",
  "lockfileVersion": 1,
  "requires": true,
  "dependencies": {
    "tildify": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/tildify/-/tildify-3.0.0.tgz",
      "integrity": "sha512-9ZLMl75qnTLr7oSEmWJbKemFS/fP4TMBiF6PFwGwLpgobebU1ehXoGbadJ+7jT8fjaz2G82JgN9G4taz+o1j1w==",
      "dev": true
    }
  }
}

I even tried removing ^ symbol from package.json to make it exact version - but that didn't help.
What I have found is if I run this command explicitly - only then the correct version 2.0.0 is installed.

npm install [email protected] --save-dev

Can anyone please help me understand why it doesn't work for npm install command?

Thank you!



Solution 1:[1]

The similar things happened to me. For me, I want to install

"@mui/lab": "^5.0.0-alpha.71"

But turns out, npm installed this for me.

5.0.0-alpha.79

That's because you have specified ^ before the version number. I removed the ^ and try npm install again, it installed excatly the version that I want. For more information about the meaning of the symbol, you can find in https://stackoverflow.com/a/25861938/15603575

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vardy