A package to bridge with multiple package managers


2 min read

Node.js now has a package called corepack which acts as a bridge ๐ŸŒ‰ ๐ŸŒ between Node.js and package managers since there are so many package managers for Node.js now - npm, and then came yarn and then pnpm came up



I noticed this just today, when I was trying to install yarn package manager and their official website recommended using corepack to install it

I installed the latest LTS version of Node.js as of this writing using nvm and checked out corepack

$ node --version

$ which corepack

$ corepack -h
โ”โ”โ” Corepack - 0.25.2 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

  $ corepack <command>

โ”โ”โ” General commands โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

  corepack cache clean
    Cleans Corepack cache

  corepack disable [--install-directory #0] ...
    Remove the Corepack shims from the install directory

  corepack enable [--install-directory #0] ...
    Add the Corepack shims to the install directories

  corepack install
    Install the package manager configured in the local project

  corepack install <-g,--global> [--cache-only] ...
    Install package managers on the system

  corepack pack [--json] [-o,--output #0] ...
    Store package managers in a tarball

  corepack up
    Update the package manager used in the current project

  corepack use <pattern>
    Define the package manager to use for the current project

You can also print more details about any of these commands by calling them with 
the `-h,--help` flag right after the command name.

yarn official docs asked me to first enable corepack and then try out yarn exec env, so I did just that

$ corepack enable

$ yarn exec env
Corepack is about to download https://registry.yarnpkg.com/yarn/-/yarn-1.22.22.tgz.

Do you want to continue? [Y/n] Y
yarn exec v1.22.22

You can see how it installed yarn on the fly ๐Ÿ˜

Then I checked out the code of yarn ๐Ÿงถ and corepack executable ;) and saw some interesting things

$ cat /Users/karuppiah.n/.nvm/versions/node/v20.12.2/bin/yarn
#!/usr/bin/env node
require('./lib/corepack.cjs').runMain(['yarn', ...process.argv.slice(2)]);

$ cat /Users/karuppiah.n/.nvm/versions/node/v20.12.2/bin/corepack 
#!/usr/bin/env node

It's pretty straight forward. I think corepack package has all the code for taking care of the installation and invoking the appropriate package manager :)

I'll probably write another blog post once I learn more about this :D