11cb0ef41Sopenharmony_ci# Corepack 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!-- introduced_in=v14.19.0 --> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci<!-- type=misc --> 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- YAML 81cb0ef41Sopenharmony_ciadded: 91cb0ef41Sopenharmony_ci - v16.9.0 101cb0ef41Sopenharmony_ci - v14.19.0 111cb0ef41Sopenharmony_ci--> 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci_[Corepack][]_ is an experimental tool to help with 161cb0ef41Sopenharmony_cimanaging versions of your package managers. It exposes binary proxies for 171cb0ef41Sopenharmony_cieach [supported package manager][] that, when called, will identify whatever 181cb0ef41Sopenharmony_cipackage manager is configured for the current project, transparently install 191cb0ef41Sopenharmony_ciit if needed, and finally run it without requiring explicit user interactions. 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciThis feature simplifies two core workflows: 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci* It eases new contributor onboarding, since they won't have to follow 241cb0ef41Sopenharmony_ci system-specific installation processes anymore just to have the package 251cb0ef41Sopenharmony_ci manager you want them to. 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci* It allows you to ensure that everyone in your team will use exactly the 281cb0ef41Sopenharmony_ci package manager version you intend them to, without them having to 291cb0ef41Sopenharmony_ci manually synchronize it each time you need to make an update. 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci## Workflows 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci### Enabling the feature 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciDue to its experimental status, Corepack currently needs to be explicitly 361cb0ef41Sopenharmony_cienabled to have any effect. To do that, run [`corepack enable`][], which 371cb0ef41Sopenharmony_ciwill set up the symlinks in your environment next to the `node` binary 381cb0ef41Sopenharmony_ci(and overwrite the existing symlinks if necessary). 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciFrom this point forward, any call to the [supported binaries][] will work 411cb0ef41Sopenharmony_ciwithout further setup. Should you experience a problem, run 421cb0ef41Sopenharmony_ci[`corepack disable`][] to remove the proxies from your system (and consider 431cb0ef41Sopenharmony_ciopening an issue on the [Corepack repository][] to let us know). 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci### Configuring a package 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciThe Corepack proxies will find the closest [`package.json`][] file in your 481cb0ef41Sopenharmony_cicurrent directory hierarchy to extract its [`"packageManager"`][] property. 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciIf the value corresponds to a [supported package manager][], Corepack will make 511cb0ef41Sopenharmony_cisure that all calls to the relevant binaries are run against the requested 521cb0ef41Sopenharmony_civersion, downloading it on demand if needed, and aborting if it cannot be 531cb0ef41Sopenharmony_cisuccessfully retrieved. 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci### Upgrading the global versions 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciWhen running outside of an existing project (for example when running 581cb0ef41Sopenharmony_ci`yarn init`), Corepack will by default use predefined versions roughly 591cb0ef41Sopenharmony_cicorresponding to the latest stable releases from each tool. Those versions can 601cb0ef41Sopenharmony_cibe overridden by running the [`corepack prepare`][] command along with the 611cb0ef41Sopenharmony_cipackage manager version you wish to set: 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci```bash 641cb0ef41Sopenharmony_cicorepack prepare yarn@x.y.z --activate 651cb0ef41Sopenharmony_ci``` 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciAlternately, a tag or range may be used: 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci```bash 701cb0ef41Sopenharmony_cicorepack prepare pnpm@latest --activate 711cb0ef41Sopenharmony_cicorepack prepare yarn@stable --activate 721cb0ef41Sopenharmony_ci``` 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci### Offline workflow 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciMany production environments don't have network access. Since Corepack 771cb0ef41Sopenharmony_ciusually downloads the package manager releases straight from their registries, 781cb0ef41Sopenharmony_ciit can conflict with such environments. To avoid that happening, call the 791cb0ef41Sopenharmony_ci[`corepack prepare`][] command while you still have network access (typically at 801cb0ef41Sopenharmony_cithe same time you're preparing your deploy image). This will ensure that the 811cb0ef41Sopenharmony_cirequired package managers are available even without network access. 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ciThe `prepare` command has [various flags][]. Consult the detailed 841cb0ef41Sopenharmony_ci[Corepack documentation][] for more information. 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci## Supported package managers 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciThe following binaries are provided through Corepack: 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci| Package manager | Binary names | 911cb0ef41Sopenharmony_ci| --------------- | ----------------- | 921cb0ef41Sopenharmony_ci| [Yarn][] | `yarn`, `yarnpkg` | 931cb0ef41Sopenharmony_ci| [pnpm][] | `pnpm`, `pnpx` | 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci## Common questions 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci### How does Corepack interact with npm? 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ciWhile Corepack could support npm like any other package manager, its 1001cb0ef41Sopenharmony_cishims aren't enabled by default. This has a few consequences: 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci* It's always possible to run a `npm` command within a project configured to 1031cb0ef41Sopenharmony_ci be used with another package manager, since Corepack cannot intercept it. 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci* While `npm` is a valid option in the [`"packageManager"`][] property, the 1061cb0ef41Sopenharmony_ci lack of shim will cause the global npm to be used. 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci### Running `npm install -g yarn` doesn't work 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_cinpm prevents accidentally overriding the Corepack binaries when doing a global 1111cb0ef41Sopenharmony_ciinstall. To avoid this problem, consider one of the following options: 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci* Don't run this command; Corepack will provide the package manager 1141cb0ef41Sopenharmony_ci binaries anyway and will ensure that the requested versions are always 1151cb0ef41Sopenharmony_ci available, so installing the package managers explicitly isn't needed. 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci* Add the `--force` flag to `npm install`; this will tell npm that it's fine to 1181cb0ef41Sopenharmony_ci override binaries, but you'll erase the Corepack ones in the process. (Run 1191cb0ef41Sopenharmony_ci [`corepack enable`][] to add them back.) 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci[Corepack]: https://github.com/nodejs/corepack 1221cb0ef41Sopenharmony_ci[Corepack documentation]: https://github.com/nodejs/corepack#readme 1231cb0ef41Sopenharmony_ci[Corepack repository]: https://github.com/nodejs/corepack 1241cb0ef41Sopenharmony_ci[Yarn]: https://yarnpkg.com 1251cb0ef41Sopenharmony_ci[`"packageManager"`]: packages.md#packagemanager 1261cb0ef41Sopenharmony_ci[`corepack disable`]: https://github.com/nodejs/corepack#corepack-disable--name 1271cb0ef41Sopenharmony_ci[`corepack enable`]: https://github.com/nodejs/corepack#corepack-enable--name 1281cb0ef41Sopenharmony_ci[`corepack prepare`]: https://github.com/nodejs/corepack#corepack-prepare--nameversion 1291cb0ef41Sopenharmony_ci[`package.json`]: packages.md#nodejs-packagejson-field-definitions 1301cb0ef41Sopenharmony_ci[pnpm]: https://pnpm.js.org 1311cb0ef41Sopenharmony_ci[supported binaries]: #supported-package-managers 1321cb0ef41Sopenharmony_ci[supported package manager]: #supported-package-managers 1331cb0ef41Sopenharmony_ci[various flags]: https://github.com/nodejs/corepack#utility-commands 134