简体   繁体   English

如何将 package.json 更新到每个 package 的最新版本?

[英]How to update package.json to latest version of each package?

Before you flag it as duplicate, I have searched for the similar questions and none of them helped me.在您将其标记为重复之前,我已经搜索了类似的问题,但没有一个对我有帮助。

Currently this is what I have tried:目前这是我尝试过的:

  1. Delete package-lock.json file.删除 package-lock.json 文件。
  2. Delete node_modules.删除节点模块。
  3. Run npm update运行npm update
  4. Run npm install运行npm install

This would always allow me to install the latest (minor) version of the packages in node_modules, and update the package-lock.json file.这将始终允许我在 node_modules 中安装最新(次要)版本的软件包,并更新 package-lock.json 文件。 However, the package.json file does not update.但是,package.json 文件不会更新。

For example, my moment is package.json is stated as "moment": "^2.27.0".例如,我的时刻是 package.json 表示为“时刻”:“^2.27.0”。 After running above steps, package-lock.json will update to "moment": { "version": "2.29.1", ...} But package.json will still be "moment": "^2.27.0".运行上述步骤后,package-lock.json 将更新为 "moment": { "version": "2.29.1", ...} 但 package.Z466DEEC76ECDF5FCA6D38571F6324D5.2 仍将是 "^24.7"。

What is the correct way to do this?这样做的正确方法是什么? Running npm install moment manually updates the package.json to become "moment": "^2.29.1" but its quite absurd if I have to run npm install for every single dependency? Running npm install moment updates the package.json to become "moment": "^2.29.1" but its quite absurd if I have to run npm install for every single dependency?

Edit Thanks to the selected answer, I realised that I do not actually need to update my package.json, as it shows compatible version, not exact version.编辑感谢选择的答案,我意识到我实际上不需要更新我的 package.json,因为它显示的是兼容版本,而不是确切的版本。

package.json will not updated by npm install . package.json不会被npm install更新。 That contains about dependencies and compatible version list.其中包含有关依赖项和兼容版本列表。

"moment": "^2.27.0" meaning allowed moment version: 2.27.0 <= version < 3.0.0 , not allowed moment version = 2.27.0 . "moment": "^2.27.0"表示allowed moment version: 2.27.0 <= version < 3.0.0allowed moment version = 2.27.0 So when you run npm install , npm will install the latest version of major version 2 (In your case, 2.29.1 ), But package.json will not updated by that command. So when you run npm install , npm will install the latest version of major version 2 (In your case, 2.29.1 ), But package.json will not updated by that command. Because It not contains installed version , It contains compatible version .因为它不包含installed version ,它包含compatible version

However, npm install moment command do install the latest version of moment , So package.json updated the latest version, because "^2.27.0" is lower than "^2.29.1" .但是, npm install moment命令确实install the latest version of moment ,所以package.json更新了最新版本,因为"^2.27.0"低于"^2.29.1" ."

Anyway, If you want to update your package.json, You can use npm-check-updates (aka ncu ).无论如何,如果你想更新你的 package.json,你可以使用npm-check-updates (又名ncu )。 See this answer .看到这个答案 If you not want running ncu , You can use "latest" (Example: "moment": "latest" ) to install the latest version anytime.如果您不想运行ncu ,您可以随时使用"latest" (例如: "moment": "latest" )安装最新版本。

npm outdated lists all packages that can be updated with the current, wanted and latest version numbers. npm outdated列出了所有可以使用当前、通缉和最新版本号更新的软件包。

  • current is the currently installed version current 是当前安装的版本
  • wanted is the last minor version update想要的是最后一个小版本更新
  • latest is the latest major version update latest 是最新的主要版本更新

To update all packages to latest just do:要将所有软件包更新到最新版本,只需执行以下操作:

npm outdated | awk 'NR>1 {print $1"@"$4}' | xargs npm install

which simply calls npm install with the latest version of each outdated package.它只是调用 npm 安装每个过时的 package 的最新版本。

It is highly recommended to check changes to your packages.json just to make sure all changes are as expected.强烈建议检查您的包的更改packages.json只是为了确保所有更改都符合预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM