DevLoverUmar
DevLoverUmar

Reputation: 14011

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

I have a react app with deprecated dependencies. 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. However, I'm using yarn for package management. Is there an equivalent of npm-check-updates in yarn. So that, I use a single package manager to manage my dependencies.

Upvotes: 216

Views: 309835

Answers (12)

Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5868

yarn upgrade-interactive --latest

But you must have a valid yarn.lock file before doing it. If you are using npm, you must delete package-lock.json at first. Then run yarn (or yarn --ignore-engines when there are version dependencies) to create structure. After that, you can do upgrade-interactive. Without that, yarn shows an upgrade, but no changes and effects in package.json.

Upvotes: 330

Pere
Pere

Reputation: 2033

Update dependencies to latest versions, using jq:

jq '.dependencies | keys | .[]' package.json | xargs yarn add

Update dev dependencies to latest versions:

jq '.devDependencies | keys | .[]' package.json | xargs yarn add --dev

Upvotes: 6

Ahmed Mokhtar
Ahmed Mokhtar

Reputation: 2516

You can try this 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.

installation:

npm install -g yarn-upgrade-all

usage: in your project directory run:

yarn-upgrade-all

Upvotes: 63

Sharif Mohammad Eunus
Sharif Mohammad Eunus

Reputation: 834

It can be done with --ignore-engines flag. 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

Upvotes: 0

sFritsch09
sFritsch09

Reputation: 442

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

Upvotes: 2

Raine Revere
Raine Revere

Reputation: 33775

npm-check-updates is fully compatible with yarn. Just run npx npm-check-updates in your project directory.

npm-check-updates is a battle-tested, 8yr old library that just works. 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 - default output

Interactive + Group mode:

npm-check-updates - interactive mode

Upvotes: 16

Ali Fensome
Ali Fensome

Reputation: 600

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.

yarn upgrade --latest

Yarn docs

Upvotes: 14

jmarceli
jmarceli

Reputation: 20182

With Yarn v2 and 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

Upvotes: 30

Arosha
Arosha

Reputation: 1401

If you want to update packages with yarn and update the package.json accordingly,

  1. Install syncyarnlock - yarn global add syncyarnlock
  2. Update packages - yarn upgrade or yarn upgrade --latest
  3. Sync updated versions of yarn.lock to package.json - syncyarnlock -s

Upvotes: 7

Amr
Amr

Reputation: 653

In case you wanted to add the package to your package.json for development collaboration

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

By the way, the package uses the command ( reinstall all packages again )

yarn install package1 package2 packageN

Upvotes: 4

Favour George
Favour George

Reputation: 1953

You can upgrade a single package to the latest major version with this:

yarn upgrade <package-name> --latest

Upvotes: 112

Inventrohyder
Inventrohyder

Reputation: 361

The one that worked for me is from a comment by @Andrew Zolotarev, which uses

npx yarn-upgrade-all

Upvotes: 24

Related Questions