/commands/yarn/yarn-upgrade-package-latest
yarn upgrade a package to the latest version (yarn upgrade --latest, yarn up)
- 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
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 --silentOutput
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
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 dependenciesOutput
{
"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
| Task | Yarn 1 | Yarn 2+ |
|---|---|---|
| List outdated packages | yarn outdated | yarn upgrade-interactive |
| Update within the package.json range | yarn upgrade pkg | yarn up -R pkg (refreshes the lock within ranges) |
| Update to latest, rewrite package.json | yarn upgrade pkg --latest | yarn up pkg |
| Update to a specific version | yarn upgrade pkg@1.2.3 | yarn up pkg@1.2.3 |
| Pick interactively | yarn upgrade-interactive --latest | yarn upgrade-interactive |
| Pattern of packages | yarn upgrade --scope @babel --latest | yarn up '@babel/*' |
| Every workspace | run in each workspace | yarn up pkg updates it in every workspace that uses it |
Pitfalls
- Major versions.
--latestandyarn upcross major versions without asking. Read the changelog, upgrade related packages together (yarn up react react-dom) and run your tests. yarn upgradewith no package name in Yarn 1 updates every dependency within its range and rewritesyarn.lock, which can be a large change; name the packages you mean.- Lockfile in CI. Commit
package.jsonandyarn.locktogether; CI with--immutableor--frozen-lockfilefails otherwise. - Resolutions win. A version forced in the
resolutionsfield ofpackage.jsonoverrides whatupgradeorupwould 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.