简体   繁体   English

Yarn - 如何将 package.json 中的每个依赖项更新到最新版本?

[英]Yarn - How do I update each dependency in package.json to the latest version?

I have a react app with deprecated dependencies.我有一个不推荐使用依赖项的 React 应用程序。 To make it work, I have to update the dependencies to their newer (but stable) versions.为了让它工作,我必须将依赖项更新到更新的(但稳定的)版本。

As per this stakoverflow thread, to update dependencies in package.json to latest versions, npm-check-updates is the Best Option for npm.根据这个 stakoverflow线程,要将 package.json 中的依赖项更新到最新版本, npm-check-updates是 npm 的最佳选择。 However, I'm using yarn for package management.但是,我正在使用 yarn 进行包管理。 Is there an equivalent of npm-check-updates in yarn. yarn 中是否有等效的 npm-check-updates? So that, I use a single package manager to manage my dependencies.因此,我使用单个包管理器来管理我的依赖项。

yarn upgrade-interactive --latest

But you have to have a yarn.lock file before do it.但是在做之前你必须有一个yarn.lock文件。 If you are using npm , you must delete package-lock.json first.如果您使用的是npm ,则必须先删除package-lock.json Then run yarn to create structure.然后运行yarn来创建结构。 After that you can do upgrade-interactive .之后,您可以执行upgrade-interactive Without that, yarn shows upgrade, but no changes and effects in package.json .没有它, yarn显示升级,但package.json没有变化和影响。

You can upgrade a single package to the latest major version with this:您可以使用以下命令将单个 package 升级到最新的主要版本:

yarn upgrade <package-name> --latest

You can try this npm package yarn-upgrade-all .你可以试试这个 npm package yarn-upgrade-all This package will remove every package in package.json and add it again which will update it to latest version.这个 package 将删除 package.Z466DEEC76ECDF25FCA6D38571F6 中的每个package.json并将其再次更新到最新版本。

installation:安装:

npm install -g yarn-upgrade-all

usage: in your project directory run:用法:在您的项目目录中运行:

yarn yarn-upgrade-all

The one that worked for me is from a comment by @Andrew Zolotarev , which uses对我有用的是@Andrew Zolotarev 的评论,它使用

npx yarn-upgrade-all

If you want to update packages with yarn and update the package.json accordingly,如果你想用 yarn 更新包并相应地更新 package.json,

  1. Install syncyarnlock - yarn global add syncyarnlock安装syncyarnlock - yarn global add syncyarnlock
  2. Update packages - yarn upgrade or yarn upgrade --latest更新包 - yarn upgradeyarn upgrade --latest
  3. Sync updated versions of yarn.lock to package.json - syncyarnlock -s将 yarn.lock 的更新版本同步到 package.json - syncyarnlock -s

List outdated清单已过时

yarn outdated

Upgrade all dependencies to latest将所有依赖项升级到最新版本

This will upgrade to the latest version, irrespective of if the package is stable or the versioning constraints between your packages.这将升级到最新版本,无论 package 是否稳定或软件包之间的版本限制。

yarn upgrade --latest

Yarn docs纱线文档

With Yarn v2 and v3 (Berry)使用 Yarn v2 和 v3 (Berry)

You have to install appropriate plugin first with:您必须先安装适当的插件:

yarn plugin import interactive-tools

and then execute然后执行

yarn upgrade-interactive

Source: https://yarnpkg.com/cli/upgrade-interactive来源: https://yarnpkg.com/cli/upgrade-interactive

In case you wanted to add the package to your package.json for development collaboration如果您想将 package 添加到 package.json 以进行开发协作

yarn add yarn-upgrade-all -D
yarn yarn-upgrade-all

By the way, the package uses the command ( reinstall all packages again )顺便说一句,package 使用命令(重新安装所有软件包)

yarn install package1 package2 packageN

npm-check-updates is fully compatible with yarn. npm-check-updates与 yarn 完全兼容。 Just run npx npm-check-updates in your project directory.只需在项目目录中运行npx npm-check-updates

npm-check-updates is a battle-tested, 8yr old library that just works . npm-check-updates是一个经过实战考验的 8 年老库,可以正常工作 It offers interactive mode and doctor mode for automatically running tests and identifying broken upgrades.它提供交互模式和医生模式,用于自动运行测试和识别损坏的升级。

Disclaimer: I am the main contributor of npm-check-updates.免责声明:我是 npm-check-updates 的主要贡献者。

npm-check-updates - 默认输出

Interactive + Group mode:互动+小组模式:

npm-check-updates - 交互模式

For the latest versions of yarn (for me it's 3.2.2)对于最新版本的纱线(对我来说是 3.2.2)

yarn up --interactive 

For more details in the official docs .更多详细信息请参见官方文档

If none of the answers worked for you, try to install again:如果所有答案都不适合您,请尝试重新安装:

yarn add {PACKAGENAME HERE}

it will overwrite the current version to the latest version available它会将当前版本覆盖为可用的最新版本

It can be done with --ignore-engines flag.可以使用--ignore-engines标志来完成。 So package installation will not fail due to an incompatible node version.因此,包安装不会因节点版本不兼容而失败。

First run:第一次运行:

yarn --ignore-engines 
// This will install the older packages

And then:接着:

yarn upgrade --ignore-engines 
// This will update packages to latest

You can use yarn upgrade.您可以使用纱线升级。 PFB link with more details on who to use it. PFB 链接,其中包含有关谁使用它的更多详细信息。

https://classic.yarnpkg.com/en/docs/cli/upgrade/ https://classic.yarnpkg.com/en/docs/cli/upgrade/

Thanks谢谢

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

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