Corepack: the Node.js’ manager of package managers

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 …

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.


Print Share Comment Cite Upload Translate
APA
Axel Navarro | Sciencx (2024-03-28T08:54:51+00:00) » Corepack: the Node.js’ manager of package managers. Retrieved from https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/.
MLA
" » Corepack: the Node.js’ manager of package managers." Axel Navarro | Sciencx - Thursday September 9, 2021, https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/
HARVARD
Axel Navarro | Sciencx Thursday September 9, 2021 » Corepack: the Node.js’ manager of package managers., viewed 2024-03-28T08:54:51+00:00,<https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/>
VANCOUVER
Axel Navarro | Sciencx - » Corepack: the Node.js’ manager of package managers. [Internet]. [Accessed 2024-03-28T08:54:51+00:00]. Available from: https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/
CHICAGO
" » Corepack: the Node.js’ manager of package managers." Axel Navarro | Sciencx - Accessed 2024-03-28T08:54:51+00:00. https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/
IEEE
" » Corepack: the Node.js’ manager of package managers." Axel Navarro | Sciencx [Online]. Available: https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/. [Accessed: 2024-03-28T08:54:51+00:00]
rf:citation
» Corepack: the Node.js’ manager of package managers | Axel Navarro | Sciencx | https://www.scien.cx/2021/09/09/corepack-the-node-js-manager-of-package-managers/ | 2024-03-28T08:54:51+00:00
https://github.com/addpipe/simple-recorderjs-demo