LinuxCommandLibrary

corepack

Manage Node.js package managers

TLDR

Add the Corepack shims to the Node.js installation directory to make them available as global commands

$ corepack enable
copy

Add the Corepack shims to a specific directory
$ corepack enable --install-directory [path/to/directory]
copy

Remove the Corepack shims from the Node.js installation directory
$ corepack disable
copy

Prepare a specific package manager
$ corepack prepare [package_manager]@[version] --activate
copy

Prepare the package manager configured for the project in the current path
$ corepack prepare
copy

Use a package manager without installing it as a global command
$ corepack [npm|pnpm|yarn] [package_manager_arguments]
copy

Install a package manager from the specified archive
$ corepack hydrate [path/to/corepack.tgz]
copy

Display help for a subcommand
$ corepack [subcommand] --help
copy

SYNOPSIS

corepack <command> [arguments...]
corepack --version | -v
corepack --help

PARAMETERS

enable
    Activates Corepack. This installs shims (proxy executables) for npm, yarn, and pnpm in a location found by your PATH, allowing Corepack to intercept calls to these package managers.

disable
    Deactivates Corepack. This removes the shims, restoring direct calls to globally installed package managers (or those bundled with Node.js).

prepare <binary-path>
    An internal command primarily used by Node.js distributions to prepare Corepack shims. Not typically used by end-users directly.

install
    An internal command used to install package managers managed by Corepack into its cache. Not typically used directly by end-users.

--version | -v
    Displays the Corepack version number.

--help
    Displays help information for Corepack.

DESCRIPTION

Corepack is an experimental Node.js tool designed to manage which package manager is used for a given project. It ensures that all team members and CI/CD environments use the exact same version of npm, Yarn, or pnpm defined by the project's packageManager field in its package.json file.

When enabled, Corepack intercepts calls to npm, yarn, or pnpm and, if a packageManager field is present in the project's package.json, it transparently downloads and uses the specified package manager version. This eliminates the need for developers to manually install or switch between different package manager versions, greatly improving project consistency and reproducibility across various development environments. It's bundled with Node.js since version 16.9.0 but often requires explicit activation.

CAVEATS

  • Corepack is bundled with Node.js 16.9.0 and newer, but is often disabled by default in many distributions (e.g., Node.js installed via OS package managers or nvm). It must be explicitly enabled using corepack enable.
  • It relies on the packageManager field in package.json. Projects without this field will fallback to globally installed or Node.js bundled package managers.
  • Corepack primarily manages Yarn and pnpm versions. For npm, it manages its version only if the packageManager field specifies an npm version different from the one bundled with Node.js.
  • Enabling Corepack creates shims that might override globally installed versions of yarn or pnpm, which can sometimes lead to unexpected behavior if not understood.

TRANSPARENT PROXYING

Once Corepack is enabled and shims are in place, invoking yarn or pnpm (or npm, if specified in packageManager) from your project's root directory will automatically delegate to Corepack. Corepack then reads the packageManager field in package.json (e.g., "packageManager": "yarn@3.2.0") and ensures that the correct version of the package manager is downloaded (if not cached) and used for the current command.

<CODE>PACKAGEMANAGER</CODE> FIELD

The packageManager field in package.json is crucial for Corepack's operation. It specifies the package manager and its exact version that a project expects. For example:
{ "name": "my-project", "version": "1.0.0", "packageManager": "yarn@3.2.0" }
or
{ "name": "my-project", "version": "1.0.0", "packageManager": "pnpm@7.1.0" }
This field acts as the single source of truth for the package manager version within the project.

HISTORY

Corepack was introduced as an experimental feature with Node.js v16.9.0. Its development was driven by the increasing need for consistent package manager versions across projects, similar to how tools like nvm or volta manage Node.js versions. By bundling it directly into Node.js, the aim was to provide a standardized, cross-platform solution to address the 'works on my machine' problem that arises when different team members use different package manager versions. It became stable and enabled by default in Node.js v19.0.0, though older Node.js versions might still require explicit enabling.

SEE ALSO

npm(1), yarn(1), pnpm(1), node(1)

Copied to clipboard