Why does `npm install` add / remove caret (^) to / from version numbers?

拥有回忆 提交于 2019-12-24 19:29:39

问题


I have a project that I work on with two different laptops. Sometimes I add extra packages to my project, so I have to use npm install <package-name> (duh). When I do that, I git push up the new package.json and package-lock.json files, and when I switch computers I have to git pull those changes, then run npm install again to get that package onto the other computer.

I recently noticed and started caring that one laptop kept adding carets (^) to the beginning of every package version number. For example:

One computer set package version #s to look like this:

"regexpu-core": {
  "version": "1.0.0",
  "resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-1.0.0.tgz",
  "integrity": "sha1-hqdj9Y7k18L2sQLkdkBQ3n7ZDGs=",
  "requires": {
    "regenerate": "1.4.0",
    "regjsgen": "0.2.0",
    "regjsparser": "0.1.5"
  }
},

The other set package version #s to look like this:

"regexpu-core": {
  "version": "1.0.0",
  "resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-1.0.0.tgz",
  "integrity": "sha1-hqdj9Y7k18L2sQLkdkBQ3n7ZDGs=",
  "requires": {
    "regenerate": "^1.2.1",
    "regjsgen": "^0.2.0",
    "regjsparser": "^0.1.4"
  }
},

I understand that carets (^) mean the version is not 100% precise, but I'm trying to figure out WHY my different laptops create different formats for package versions! I checked this SO question which has some great explanations for the differences between ~ and ^, but I didn't find anything explaining why npm would sometimes add and sometimes remove carets (^) altogether. I also looked at this npm issue on Github which recommended looking at npm config settings, but both of my laptops have the same settings:

  • npm config get save = true (both computers)
  • npm config get save-prefix = ^ (both computers)
  • npm config get save-exact = false (both computers)

One laptop was running npm version 5.6.0, but I just updated it to 6.5.0. The other computer was running version 6.4.1, but I also updated it to 6.5.0. I tried running npm install in my project on both computers, but still I find that one computer always removes ^ and the other always adds ^.

Please let me know if there's something I'm missing. Thanks for any help!


回答1:


After you git pull the revised package.json and package-lock.json onto computer two try deleting the node_modules directory before installing the packages again.

For example:

  1. Firstly cd to your project directory on computer 2.

  2. Delete the existing node_modules directory by running: rm -rf node_modules.

  3. Then run: npm install

Or you can chain the two aforementioned commands using the && operator:

rm -rf node_modules && npm install


来源:https://stackoverflow.com/questions/54091452/why-does-npm-install-add-remove-caret-to-from-version-numbers

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