Docs/Intermediary concepts/Node.js versioning

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 (the engines declared through version files are lazy by default):

{
  "dependenciesMeta": {
    "@yarnpkg/node": {
      "lazy": true
    }
  }
}

Node.js is then only downloaded 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. Those are downloaded when they’re added to the project, so that the lockfile records their checksum, and then only when called on a fresh clone; Node.js skips the first download, since its builds are specific to each platform and the lockfile doesn’t record their checksums.

The version files#

Most projects want a single Node.js version for all their scripts. Instead of declaring @yarnpkg/node yourself, you can write it in a .node-version file at the root of the project, the file version managers such as nvm or fnm read too (new projects created with yarn init get one with the latest major of Node.js). Python works the same way, with the .python-version file pyenv and uv read:

echo 24 > .node-version
echo 3.13 > .python-version

Yarn reads the first version of each file as a semver range: 24 stands for the latest 24.x release, 24.11.1 for that exact release, and the v (v24) and cpython- (cpython-3.13) prefixes are accepted. What it can’t use as a version (lts/*, system, pypy3.10, …) is ignored with a warning, since other tools may understand it.

Each version file makes Yarn add the matching builtin package (@yarnpkg/node, or @yarnpkg/python for the python-build-standalone CPython builds) to the dependencies of the root workspace, and the lockfile records the engines along with the rest of the project. Its binaries (node, or python and python3) are then added to the PATH of every script of the project: those of the root workspace, but also those of the other workspaces and the build scripts of your dependencies. They don’t become dependencies of these packages, which keeps the dependency tree as it is.

The engines are lazy: a fresh clone only downloads them the first time one of their binaries is called, so projects whose scripts never run Node.js or Python don’t pay for them. Setting dependenciesMeta["@yarnpkg/node"].lazy (or @yarnpkg/python) to false downloads them with the other packages instead. The Python engine is always downloaded when venv islands use it as their interpreter, since the install needs it.

The engines are a default: a package whose own dependencies include @yarnpkg/node runs its scripts with that version instead, and the root workspace can declare @yarnpkg/node itself to pick a different version than the engine would. Like other development dependencies, the engines aren’t installed by production installs.

A same Python version may be built in several python-build-standalone releases, so the release is part of what the lockfile records (@yarnpkg/python@builtin:3.13.1-20241206 is Python 3.13.1, from the 20241206 release). Ranges are resolved against the latest release, which contains the latest patch of each Python version it supports; to pin a build from an older release, use such a version as range. The builds are downloaded from the pythonDownloadMirror setting.

The Python engine is also the Python version of the islands that don’t set their own pythonVersion: their PyPI packages are resolved for it, and their venvs are created from it.

Island version files#

Islands can use other versions than the rest of the project: a .node-version or .python-version file in the folder of one of the workspaces of an island overrides the one at the root of the project for the whole island. The other workspaces keep the engines of the project.

.node-version            24
.python-version          3.13
packages/
  legacy-api/
    .node-version        20     (the island of legacy-api uses Node.js 20)
    .python-version      3.11   (and Python 3.11)

The engines of an island are development dependencies of its workspaces, resolved along with the rest of the island and recorded in the island’s part of the lockfile; they’re downloaded with the island rather than lazily. An island only has one version of each engine, so its workspaces can’t declare different ones. The Python version of an island’s .python-version is the one its PyPI packages are resolved for and its venvs are created from, unless the island sets its own pythonVersion.

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 install and 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.