Skip to content

/commands/yarn/yarn-upgrade-package-latest

yarn upgrade a package to the latest version (yarn upgrade --latest, yarn up)

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Yarn
versions
Yarn 1.x, Yarn 2+, Yarn 4
os
any

In Yarn 1, yarn upgrade is-number --latest upgrades a package to the version tagged latest, ignoring the range in package.json, and saves the new version; yarn outdated lists what is behind first. In Yarn 2 and later the command is yarn up is-number, which always resolves to the latest version and rewrites the range in package.json; yarn upgrade no longer exists there.

Yarn 1: outdated, then upgrade --latest

bash
echo '{ "name": "app", "version": "1.0.0", "license": "MIT", "private": true }' > package.json
Y="npx --yes yarn@1"
$Y add --silent --no-progress is-number@6.0.0
$Y outdated --no-progress | grep -E "^(Package|is-number)" | sed -E 's/ +URL *$//; s/ +https:.*$//'
$Y upgrade is-number --latest --silent --no-progress
npx --yes yarn@1 list --depth=0 --pattern is-number --silent

Output

Package   Current Wanted Latest Package Type
is-number 6.0.0   6.0.0  7.0.0  dependencies
└─ is-number@7.0.0

yarn outdated shows Current, Wanted (the newest version inside the package.json range) and Latest, and exits with code 1 when something is outdated. Without --latest, yarn upgrade pkg only moves to Wanted, which for an exact 6.0.0 means no change. The example runs Yarn through npx yarn@1; with Yarn installed, drop the npx --yes part.

Yarn 4: yarn up

bash
echo '{ "name": "app", "version": "1.0.0", "license": "MIT", "private": true }' > package.json
touch yarn.lock
Y="npx --yes @yarnpkg/cli-dist@4"
$Y add is-number@6.0.0 > /dev/null
$Y up is-number > /dev/null
npm pkg get dependencies

Output

{
  "is-number": "^7.0.0"
}

yarn up replaced the exact 6.0.0 with the latest release and a caret range, because it always resolves the newest version. To keep the old exact style add -E, and to move to a specific version name it: yarn up is-number@6.0.0.

Command map

TaskYarn 1Yarn 2+
List outdated packagesyarn outdatedyarn upgrade-interactive
Update within the package.json rangeyarn upgrade pkgyarn up -R pkg (refreshes the lock within ranges)
Update to latest, rewrite package.jsonyarn upgrade pkg --latestyarn up pkg
Update to a specific versionyarn upgrade pkg@1.2.3yarn up pkg@1.2.3
Pick interactivelyyarn upgrade-interactive --latestyarn upgrade-interactive
Pattern of packagesyarn upgrade --scope @babel --latestyarn up '@babel/*'
Every workspacerun in each workspaceyarn up pkg updates it in every workspace that uses it

Pitfalls

  • Major versions. --latest and yarn up cross major versions without asking. Read the changelog, upgrade related packages together (yarn up react react-dom) and run your tests.
  • yarn upgrade with no package name in Yarn 1 updates every dependency within its range and rewrites yarn.lock, which can be a large change; name the packages you mean.
  • Lockfile in CI. Commit package.json and yarn.lock together; CI with --immutable or --frozen-lockfile fails otherwise.
  • Resolutions win. A version forced in the resolutions field of package.json overrides what upgrade or up would choose.

How to confirm it worked

Check package.json for the new range, then yarn why pkg for the version actually installed. In Yarn 1, yarn outdated printing no package rows and exiting with 0 means nothing is left to upgrade.

FAQ

Why does yarn upgrade say Unknown command in Yarn 4?

yarn upgrade exists only in Yarn 1. Yarn 2 and later replaced it with yarn up, which always moves the package to the latest version and rewrites package.json.

How do I see outdated packages in Yarn 2+?

There is no built-in yarn outdated in Yarn 2+. Run yarn upgrade-interactive, which lists outdated packages and lets you pick the ones to upgrade; it is built in from Yarn 4.

How do I upgrade a transitive dependency with Yarn 4?

Use yarn up -R package (--recursive). It refreshes the locked versions of the package everywhere in the tree, within the ranges the parents allow, without adding it to package.json.

Sources