Skip to the solution
EasyBackend6 min read

What is Corepack and how does it manage npm, Yarn, and pnpm versions?

613 views

The problem

Question presented to candidate: "A teammate's install produces a different lockfile than yours. What tool exists to stop that?"

What a strong answer should cover:

  • Corepack ships bundled with Node.js itself (since Node 16.9, promoted further since) β€” it is not something you npm install separately.
  • It does not implement npm, Yarn, or pnpm itself. It is a thin shim: when you run yarn or pnpm, Corepack intercepts the command and downloads/runs the exact version the project declares.
  • That version is declared in package.json's "packageManager" field β€” e.g. "packageManager": "pnpm@9.1.0" β€” so it travels with the repo, not with each developer's global install.
  • The practical problem it solves: without it, one developer's globally-installed Yarn 1 and another's Yarn 4 produce different lockfiles and different install behaviour from the same package.json.
  • It must be enabled once per machine (corepack enable) before it will intercept commands; a project declaring a packageManager field does nothing on a machine where Corepack is disabled.
  • Its status is not settled forever β€” a version of Node briefly shipped it as opt-out-by-default and then reversed course to opt-in; version-specific defaults should be checked against the Node release actually in use rather than assumed.

Clarifying questions expected:

  • "Which Node version is the team standardised on, and is Corepack enabled there?" β€” the defaults have shifted between releases.
  • "Is the packageManager field actually committed to package.json?" β€” the whole mechanism does nothing without it.

Code / implementation expected: Optional. Showing the packageManager field and the enable command is the substance.

  • corepack
  • npm
  • yarn
  • pnpm
  • tooling

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 10 sections

Read the code

Pinning and enabling a package manager version
$ corepack --version
0.35.0

# In package.json:
#   "packageManager": "pnpm@9.1.0"

# Once per machine, before it will intercept anything:
$ corepack enable

# Now every "pnpm install" in this repo runs pnpm 9.1.0,
# regardless of what pnpm version is installed globally.

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 16 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

What is Node.js and why is it popular?

Continue