iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💨

Specifying the npm client per module with Corepack

に公開

tl;dr

  • Node.js 14.19.0 introduced Corepack, which allows you to explicitly switch npm versions.
  • Currently, npm-cli itself does not support Corepack, so if you want to enable it, you need to move the npm command itself to Corepack.
  • At this point, simply specifying packageManager won't make it active in other environments, but it will gradually become effective once Corepack support in npm and node is widely adopted.

More Details

# Update your local Node.js to v14.19.0 or higher
# I installed it using nvm, so I did this:
$ nvm install v14.19.0

# It won't work depending on path priority unless you remove globally installed npm, pnpm, and yarn
$ npm uninstall -g npm pnpm yarn
# At this point, the npm command is no longer available

# Enabling via corepack will create npm, pnpm, and yarn managed by corepack
$ corepack enable npm pnpm yarn

# At this point, the default npm pre-installed in Node.js is restored
$ npm -v 8.3.2

(I'm not sure how the npm binary itself behaves. You might need to do something like corepack prepare npm@8.3.2 --activate before restoring npm.)

Now for the main part: explicitly specifying the npm client for each package.json.

mkdir foo && cd foo

code foo/package.json
{
  "packageManager": "npm@6.29.0"
}
# Running npm in a directory with this package.json as a parent will use the specified version
$ npm -v
6.29.0

# pnpm, yarn, etc., are no longer available (actually, corepack is managing this)
$ pnpm
Usage Error: This project is configured to use npm
$ yarn
Usage Error: This project is configured to use npm

$ cd .. # Move outside the directory containing the package.json

$ npm -v
8.3.2

If you want to use pnpm or Yarn, it's best to let Corepack manage them. However, unlike npm, they already have implementations that look at packageManager, so it's less likely to be an issue compared to npm.

Why You Should Use Corepack

In team development, even when everyone is supposed to be using Yarn, someone might accidentally use npm, leading to incorrect states being pushed to the repository.

Also, even if you only intend to use npm, you might run into incompatibilities between v6 and v8. In the current Node ecosystem, the engines field in package.json is often casually ignored, so it's easy for versions to get mixed up if you're not careful. While some issues can be detected with npm ci, there are times when you're forced to avoid even that due to npm's unreliability (for instance, it sometimes locks environment-specific optional binaries depending on the steps taken...).

Corepack can resolve this. It doesn't mean npm i will fallback to yarn install, but at least if Corepack is enabled, it can detect the mistake of running npm install.

...That said, introducing it now might not stop inquiries like "What is this error?" during team development. However, it's better than having an incorrect state pushed. Anticipating the time when it's widespread and npm itself officially supports it, and since there's no harm in using it now, it seems like a good idea to prioritize adopting it.

Discussion