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

https://www.npmjs.com/package/corepack

https://github.com/nodejs/corepack

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
v20.12.2

$ which corepack
/Users/karuppiah.n/.nvm/versions/node/v20.12.2/bin/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
NODE=/Users/karuppiah.n/.nvm/versions/node/v20.12.2/bin/node
INIT_CWD=/Users/karuppiah.n/projects/github.com/ola-platforms/kraken
NVM_INC=/Users/karuppiah.n/.nvm/versions/node/v20.12.2/include/node
NVM_RC_VERSION=
...
...

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
process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='1'
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
process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT??='0';
require('./lib/corepack.cjs').runMain(process.argv.slice(2));

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

References:

ย