All Articles

How To Use Corepack

Matt Pocock
Matt PocockMatt is a well-regarded TypeScript expert known for his ability to demystify complex TypeScript concepts.

I've just learned about corepack, a tool that bundles with Node.js and solves a bunch of problems with handling package managers. But I'll be using it in my development setup from now on.

Quick Start

Step 1. Global Install

corepack is bundled with Node.js, and has been since Node.js 14.19. So, if you have Node.js, you have corepack.

You can enable corepack on your machine by running the following command:

corepack enable && corepack enable npm

This enables corepack globally - you don't need to enable it per project.

Step 2. Configure Your Project

corepack makes sure you're using the correct package manager for your project. To configure the package manager for your project, add the packageManager field to your package.json:

{
  // npm
  "packageManager": "npm@10.8.1",
  // pnpm
  "packageManager": "pnpm@9.1.4",
  // yarn
  "packageManager": "yarn@3.1.1"
}

You must specify an exact version of the package manager you want to use - not a range. All of the below are not valid:

{
  // not valid: uses a range
  "packageManager": "npm@^10.8.1",

  // not valid: specifies 'latest'
  "packageManager": "pnpm@latest",

  // not valid: must specify an exact version
  "packageManager": "yarn"
}

Step 3. Try It Out

Now, if you try to npm install in a project that has packageManager set to pnpm, corepack will show an error:

Usage Error: This project is configured to use pnpm

$ npm ...

And if you try to pnpm install there, corepack will automatically download and use the correct pnpm version:

Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-9.1.4.tgz.

Do you want to continue? [Y/n]

This ensures you're always using the correct package manager for your project.

Why Do We Need corepack enable npm?

corepack intercepts calls to pnpm and yarn to make sure you're using them correctly. This is set up by running corepack enable.

Without running corepack enable npm, you won't get the same validation when using npm. So, we run corepack enable npm to make sure that npm gets treated the same way as pnpm and yarn.

Why Use Corepack?

I ran out of time to continue writing this article. Want me to keep going? What questions do you have? Let me know:

Matt's signature

Share this article with your friends

`any` Considered Harmful, Except For These Cases

Discover when it's appropriate to use TypeScript's any type despite its risks. Learn about legitimate cases where any is necessary.

Matt Pocock
Matt Pocock

No, TypeScript Types Don't Exist At Runtime

Learn why TypeScript's types don't exist at runtime. Discover how TypeScript compiles down to JavaScript and how it differs from other strongly-typed languages.

Matt Pocock
Matt Pocock