The best way to do this is using Node Corepack, as Yarn docs suggests. This enables you to run modern versions of Yarn (2+) without the need of having Yarn Classic (1) installed.
First, add packageManager
to your package.json
, for example:
"packageManager": "yarn@3.6.0"
(this should work both for all modern versions of Yarn: 2, 3, 4, and possibly future versions)
Once you do this, you'll no longer need to keep Yarn binary in your repo. You can safely remove yarnPath
from yarnrc.yml
and you can remove the contents of .yarn/releases
folder.
Then (assuming you have Node 14+ installed), you may need to enable Corepack by running:
corepack enable
If everything was successful, running
yarn -v
in your project should now return correct version number.
Note that the first time you use a binary specified in packageManager
in package.json
Node Corepack will transparently download it in the background, so you need internet access and the first command may take a couple of seconds to complete. After that, you'll be able to use the binary without internet access and without any delays.
Now that you've verified your setup works correctly on your local machine, it's time to do the same on Azure Pipelines. Here's the full setup, from nothing to your first Yarn-powered command:
# …
stages:
- stage: runTests
displayName: 'Run tests'
jobs:
- job: runLint
displayName: 'ESLint'
steps:
# Cache .yarn/cache for faster installs
- task: Cache@2
inputs:
key: 'yarn | $(Agent.OS) | yarn.lock'
restoreKeys: |
yarn | $(Agent.OS)
yarn
path: .yarn/cache
displayName: Cache .yarn/cache
# Install Node.js - all versions from version 14 ship with Corepack
- task: NodeTool@0
inputs:
versionSpec: '18.x'
displayName: 'Install Node.js'
# Enable Corepack
- script: corepack enable
displayName: 'Enable Corepack'
# Install dependencies
- script: yarn --immutable
displayName: 'Install dependencies'