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. 📦
Installation
The new Node.js LTS v16 will be released at the end of October (without a fancy name assigned yet), it'll have Corepack preinstalled in the default configuration since v16.9.0. 👌
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 pnpm
, npm
and yarn
.
Basic usage
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.
Specific versions
The included 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 nvm
.
$ corepack prepare npm@6.14.15 --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 npm@7
.
🤔 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
.
Global package managers 🌎
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 enable
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
or yarn
directly:
$ pnpm -v
6.11.0
Docker images
At the moment, the official Node.js images in Docker comes with npm
and yarn
preinstalled, but this may change in the near future. (no more npm
and yarn
preinstalled in the Docker image is possible! 🤯)
If you use pnpm
in Docker you need to install it using npm
or curl
:
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 npm@6.14.15 --activate && corepack npm ci
Conclusion
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. 🔥
Also, 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.
Top comments (1)
Useful thanks!