Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make `npm install --save` add a strict version to package.json

When you run npm install --save somepackage, it usually adds something like this into package.json:

"dependencies": {     "somepackage": "^2.1.0" } 

Because the version is prepended with a caret(^), this means that if you later run npm install, it might install version 2.3.0 instead. This can be undesirable for fairly obvious reasons. npm shrinkwrap is useful, but doesn't really solve the problem.

So, I have several questions:

  1. When installing a package, is it possible to specify that you want it to be set to a specific version in package.json (no caret before the version number)?
  2. When publishing a package to npm, is there any way to prevent the default of including the caret before the version when other developers install your package?
like image 726
twiz Avatar asked Jun 05 '15 00:06

twiz


People also ask

Does npm install add to package json?

When you (or another user) run npm install , npm will download dependencies and devDependencies that are listed in package. json that meet the semantic version requirements listed for each.

How do I install a specific version of npm?

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.

Can I change version in package json?

You need to add --save to the command in both install and uninstall cases. This way, when uninstalling with --save, the package's line will be erased from package. json as well as from node_modules. And when installing with --save, the package's line will be added to the package.

Does npm install modify package json?

npm install will generate a new package-lock. json if it does not exist or it will update the dependency tree if it does not match the packages specified in the package. json . npm ci will install packages based on package-lock.


2 Answers

To specify by default a exact version, you can change your npm config with save-exact:

npm config set save-exact true 

You can also specify the prepend version with a tilde with save-prefix.

And, no you can't force user to update to a minor or a patch version, NPM uses semver and it's the recommend way of publishing packages.

like image 143
Pierre Inglebert Avatar answered Sep 28 '22 03:09

Pierre Inglebert


You can change the default behaviour by using the --save-exact option.

// 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/blog/how-to-save-exact-npm-package-versions/

like image 37
Dale Jefferson Avatar answered Sep 28 '22 03:09

Dale Jefferson