How can I update global packages in Yarn?

I tried some possible CLI commands but none seem to actually update the packages installed with yarn global add.

yarn global upgrade & yarn upgrade global both don't work correctly. Is there a way of upgrading global packages?

40783 次浏览

There has been an issue created for this already at https://github.com/yarnpkg/yarn/issues/776

Using yarn global add <package>@latest will upgrade a specific package if that is what you are trying to do.

Update

The recently added yarn global upgrade upgrades all packages. This did not exist at the time of the original answer.

TL;DR:

As webjay says, you simply:

yarn global upgrade

in yarn version 1.2.1 onwards.

For earlier versions:

(cd ~/.config/yarn/global && yarn upgrade)

Checking and repairing

Sadly, there is currently no yarn global check.

You can run yarn global add --force to reinstall all packages.

To check global packages, you can treat ~/.config/yarn/global/ like a local package, since:

  • ~/.config/yarn/global/package.json has dependencies for all global packages
  • ~/.config/yarn/global/node_modules contains all the global packages.

Check all global packages, and reinstall only if an error is found:

$ (cd ~/.config/yarn/global && yarn check || yarn install --force)

I am using yarn global upgrade

Note that per yarnpkg/yarn #5001, yarn global upgrade foo does not always upgrade a package to the latest version, even with the --latest flag.

For example, if you have foo v12.0.1 installed and v13.0.0 is available, this would not upgrade to v13.0.0 because yarn global upgrade currently does not go past major bumps in semver by default (see discussion in above issue for more).

Running yarn global add foo does upgrade the currently installed version to the latest even if there is a semver major version bump between the currently installed version and the latest version.

I believe that yarn global upgrade-interactive would allow one to upgrade past major version bumps as well.

I was getting an error when attempting to upgrade. This started occurring after installing yarn 2.

I was trying to upgrade my globally installed yarn packages via:

yarn global upgrade

Apparently yarn global has been replaced with yarn dlx, however yarn dlx does not do the same thing as yarn global. How confusing :/.

The trick for me was to temporarily downgrade my installation via:

yarn set version classic

Now I can run the upgrade command.

After that, I updated again via:

yarn set version berry