Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 9, 2021 01:23 pm GMT

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 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 [email protected] for one project and [email protected] for another?

Well, in this scenario you'll will find corepack very similar to nvm.

$ corepack prepare [email protected] --activate$ node -vv16.9.0$ npm -v7.21.1$ corepack npm -v6.14.15

So with corepack prepare you'll setup [email protected], 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 [email protected].

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 [email protected] 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 -v6.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 [email protected] --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.


Original Link: https://dev.to/cloudx/corepack-the-node-js-manager-of-package-managers-44dd

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To