Node.js versioning
Pin the version of Node.js used in your application
One of the subtle causes of “works on my machine” bugs comes from differences in Node.js versions between developers. While tools like nvm, fnm, or Volta help manage local Node.js installations, they require each team member to manually configure their environment. Yarn takes a different approach by letting you declare Node.js as a project dependency, ensuring everyone uses exactly the same version without any extra setup.
The @yarnpkg/node dependency#
Yarn provides a special @yarnpkg/node package that you can add to your dependencies just like any other package. When installed, Yarn will download the appropriate Node.js binary for your platform directly from nodejs.org and make it available to your project.
{
"name": "my-app",
"dependencies": {
"@yarnpkg/node": "builtin:^22.0.0"
}
}
The range here works similarly to semver ranges - Yarn will resolve it to the highest available Node.js version that satisfies your constraint. Once resolved, this version gets locked in your yarn.lock file, guaranteeing that every developer and CI environment uses the exact same Node.js release.
Since the version is locked, new Node.js releases are only picked up when you ask for them:
# Move the lockfile to the latest release matching the range you declared
yarn up -R @yarnpkg/node
# Replace the range by the latest Node.js release
yarn up @yarnpkg/node
# Replace the range by a specific one
yarn up @yarnpkg/node@builtin:^24.0.0
Downloading Node.js only when it’s needed#
Node.js weighs over a hundred megabytes. If some of the people or CI jobs cloning the project never run JavaScript, mark it as lazy in the root package.json:
{
"dependenciesMeta": {
"@yarnpkg/node": {
"lazy": true
}
}
}
Yarn still downloads it when it’s added to the project, so that the lockfile records its checksum. A fresh clone, though, only downloads it the first time node is called, whether by a script, yarn node, or the build scripts of your dependencies. yarn install --fetch-lazy downloads it upfront, for example when preparing a CI image. Any package with binaries can be made lazy the same way.
Why manage Node.js through Yarn?#
There are several advantages to managing Node.js as a dependency:
-
Zero configuration - CI and team members don’t need to install nvm or any other version manager. Just
yarn installand they’re ready to go. -
Version locking - The exact Node.js version is recorded and cached along with all other dependencies in your project.
-
Per-project versions - Different projects can use different Node.js versions without any manual switching. Yarn handles it automatically.
-
Per-workspace versions - You can easily override the Node.js version to use for a single workspace or a set of workspaces through profiles.
Using the managed Node.js#
Once installed, the managed Node.js binary is available through the node binary that Yarn injects into your environment. You can use it in several ways:
Through Yarn scripts#
Package scripts automatically use the project’s Node.js version:
{
"scripts": {
"start": "node server.js"
}
}
Through yarn node / yarn exec#
Both commands will run Node.js with the correct environment setup:
yarn node --version
yarn node script.js
yarn exec node --version
Monorepo support#
In a monorepo, you typically want all workspaces to use the same Node.js version. Rather than adding @yarnpkg/node to each workspace’s package.json, you can use workspace profiles to declare it once:
workspaceProfiles:
default:
devDependencies:
"@yarnpkg/node": "builtin:^22.0.0"
Since the default profile is automatically applied to all workspaces, every package in your monorepo will use the same Node.js version without any additional configuration. This keeps your Node.js version centralized and easy to update.
To use another Node.js version in some of the workspaces, declare it in a profile of its own:
workspaceProfiles:
default:
devDependencies:
"@yarnpkg/node": "builtin:^22.0.0"
legacy:
devDependencies:
"@yarnpkg/node": "builtin:^20.0.0"
The workspaces that list this profile in the extends field of their package.json ("extends": ["legacy"]) use its version, since the profiles a workspace extends take precedence over the default one; the other workspaces keep the version of the default profile. A single workspace can also list @yarnpkg/node in its own devDependencies, which wins over every profile.
Platform support#
The @yarnpkg/node package automatically downloads the correct binary for your operating system and architecture. Currently supported platforms include:
- Linux (x64, arm64), both glibc and musl
- macOS (x64, arm64)
When working in a team with mixed platforms, Yarn will store metadata about all required platform variants in the lockfile, but each developer will only downloads the binary they need for their platform (configurable through supportedArchitectures).
This ensures the lockfile remains consistent across the entire team while keeping installs as lightweight as possible.
Alpine and other musl-based systems#
The official Node.js builds for Linux are linked against glibc, and won’t start on systems built on musl, the most common of which is Alpine. The Node.js project publishes musl builds separately, as part of its unofficial builds, and those are the ones Yarn installs there. There’s nothing to configure: Yarn detects the libc of the system it runs on, the same way it does to pick the right binaries of any other package.
Both kinds of builds are locked together, so the lockfile is the same wherever it’s generated: an Alpine container will run the musl build, your teammates on Ubuntu or macOS the official ones.
The unofficial builds are made after the releases they follow, and not for all of them. When Yarn resolves a range on a musl-based system it only considers the releases that have a musl build; on other systems it can’t tell, and may lock a release your Alpine containers won’t be able to install until its musl build is published. Yarn will tell you when that happens.
Both sources can be pointed at mirrors, through the nodeDistUrl and nodeUnofficialDistUrl settings.