Corepack is the new built-in tool for preparing the required package manager for our projects. Let's see what comes with this new package for packagers. 📦
If we want to use this tool with previous versions we just need to install it as a global package:
$ npm install -g corepack
🧠 Remember that for now Corepack v0.9.0 only supports
We can use the same package manager that we've always use in any machine without worrying if it's installed or not, using:
$ corepack pnpm install
Corepack will see to it that the required manager is ready to process your request.
npm version with Node.js LTS v16 is v7, so what if we need
npm@6 for one project and
npm@7 for another?
Well, in this scenario you'll will find
corepack very similar to
$ corepack prepare email@example.com --activate $ node -v v16.9.0 $ npm -v 7.21.1 $ corepack npm -v 6.14.15
🤯 So with
corepack prepare you'll setup
npm@6, and you can use it every time you need just by prefixing your
npm usage with Corepack, e.g.
corepack npm ci.
And your global
npm command will continue untouched being
🤔 But what is that
--activate flag? Corepack will keep a cache of used versions of a packager, so you should activate the specific version that you want to use; otherwise you will still be using
npm@7 when you use
corepack npm -v.
What if I want
pnpm as a global command in my terminal? You can enable Corepack to handle
pnpm and automatically install it when is used for the first time:
Corepack will install the handlers in the node directory because it's in the
$PATH, but you can use the destination of your choice:
corepack enable --install-directory path/to/bins_folder
And it's done, you can use
$ pnpm -v 6.11.0
At the moment, the official Node.js images in Docker comes with
yarn preinstalled, but this may change in the near future. (no more
yarn preinstalled in the Docker image is possible! 🤯)
If you use
pnpm in Docker you need to install it using
RUN npm install -g pnpm && pnpm install
Now Corepack is here to make this easier for you:
RUN corepack pnpm install
Or maybe you need a specific version like this:
RUN corepack prepare firstname.lastname@example.org --activate && corepack npm ci
With Corepack we can have more control about which package manager we're using and we don't need to check if Node.js upgrades
npm and we don't notice. Well, if that happens, by the time we realize that our application is on fire. 🔥
npm is part of GitHub Inc. and it's not part of the Node.js governance, so it's a good decision if the Node.js project becomes agnostic and as developers we can use the package manager that we need without wasting space in our Docker images on packagers that we won't use.