npm - Semver versioning - Updating a package with a caret “^”

ぐ巨炮叔叔 提交于 2019-12-24 10:18:35

问题


I have a npm package in my package.json file

 "clean-webpack-plugin": "^0.1.18"

Now when I hover over the package I can see that there is a newer version

"clean-webpack-plugin": "^0.1.19"

Now, as I understood, I could for example do npm update to update all packages obeying the semver rules or just the package npm update clean-webpack-plugin.

So the caret ^ symbol should mean, that you could possibly update the packge to version 0.9.9 if available, right?

npm update has no effect, that's why I ask.


回答1:


I'm quite certain that npm will have updated the application files for clean-webpack-plugin from version 0.1.18 to version 0.1.19 after you run: npm update clean-webpack-plugin as described in your question.

However, npm will not have updated the entry in your package.json as theoretically it's not actually necessary to do so. Why?.. because version "^0.1.18" is specified in package.json. I.e. The version is specified with the caret ^ symbol.

Let's say your were to publish your project with ^0.1.18 specified in package.json then any subsequent user running npm install will actually get version 0.1.19 anyway (caveat: as the version history for clean-webpack-plugin currently stands in the npm repository at the time of writing this).

So, in short I'm quite sure that version 0.1.19 has been installed on your system, it simply hasn't changed the version specified in package.json. It's not actually necessary to do so and the rules of semver and the use of the caret symbol are still being respected.

So the caret ^ symbol should mean, that you could possibly update the package to version 0.9.9 if available, right?

The caret in "^0.1.18" is saying to npm I WILL accept any updates to the most recent MINOR version but I WILL NOT accept a MAJOR update. I.e. ^0.1.18 means any updates in the range >=0.1.18 <1.0.0 (PATCH updates within that range are allowed too).


Verifying whether it has been updated:

To verify whether version 0.1.19 has actually been installed you can cd to your project directory and run:

npm ls clean-webpack-plugin

You should see the following logged to your console:

...
└── clean-webpack-plugin@0.1.19

But I want package.json to show "^0.1.19" after running npm update:

When you initially run npm update clean-webpack-plugin you could have:

  1. Appended the --save-dev argument (applicable if it was listed in your devDependencies section of package.json).
  2. Or, appended the --save argument (applicable if it was listed in your dependencies section of package.json).

Appending either --save-dev or --save as appropriate to npm update clean-webpack-plugin would have update the entry in package.json. This is further explained in the Recording Updates with --save section of the npm documentation.

By doing this, you can think of it as re-specifying the initial >= part of the range of updates you'll accept.


Note

Running npm update clean-webpack-plugin with the additional --save or --save-dev argument will not have any affect if npm ls clean-webpack-plugin reports:

...
└── clean-webpack-plugin@0.1.19

There would be nothing to update, so I'd just manually specify "^0.1.19" in package.json.



来源:https://stackoverflow.com/questions/49270776/npm-semver-versioning-updating-a-package-with-a-caret

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!