What is Corepack and its functionality in Node.js

What is Corepack and its functionality in Node.js

Daily short news for you
  • A software that converts text to speech created by a Vietnamese programmer - J2TEAM - Text to Speech (Free). You can convert dozens of languages into dozens of different natural voices. The special thing is that it is free.

    In preliminary evaluation, the conversion of long texts or texts in pure Vietnamese is very good. However, when it includes English words, it sounds a bit funny 😅

    » Read more
  • How terrifying, Codeium - known as a competitor to Github Copilot, as it allows users to use it for free without limits. Recently, they introduced the Windsurf Editor - no longer just a VSCode Extension but a full Editor now - directly competing with Cursor. And the main point is that it... is completely free 🫣.

    » Read more
  • There is a rather interesting study that I came across: "Users never bother to read things they don't want to." (That's a bold statement, but it's more true than not. 😅)

    Don't believe it? I bet you've encountered situations where you've clicked on a button repeatedly and it doesn't respond, but in reality, it has displayed an error message somewhere. Or you've filled out everything and then when you hit the submit button, it doesn't go through. Frustrated, you scroll up or down to read and find out... oh, it turns out there's an extra step or two you need to take, right?

    It’s not far from the blog here. I thought that anyone who cares about the blog would click on the "Allow notifications" button just below the post. But the truth is, no one bothers to click it. Is it because they don't want to receive notifications? Probably not! I think it's because they just didn’t read that line.

    The evidence is that only when a notification pops up and takes up half the screen, or suddenly appears to grab attention, do they actually read it—and of course, it attracts a few more subscribers—something that was never achieved before.

    » Read more

Problem

npm has been the default package manager bundled with Node.js for a long time. Most of us use npm to download packages from the npm registry, or whenever we need to reinstall all the dependencies used in a project. npm has high security mechanisms such as access to private packages, two-factor authentication and verification...

Convenient as it is, npm also has some limitations such as slow package installation speed, creating node_modules folders with the equivalent size of a black hole... along with a complex configuration. That's why many other package managers have emerged to overcome these weaknesses, such as Yarn or pnpm.

While Yarn is renowned for its package installation speed, pnpm has the mechanism of reducing the size of node_modules by sharing directories. Now we don't necessarily have to use npm by default, but can choose other package managers according to preferences or project needs.

Another good news is that starting from Node.js 14, we don't need to manually install Yarn or pnpm anymore as it has been integrated into Node.js with an experimental flag (Stability: 1) named Corepack. It may take a while for Corepack to be assigned a stable flag, but right now let's find out what Corepack is and how to use it.

What is Corepack?

Corepack is currently an experimental tool that helps manage package managers. It acts as a proxy. When called, it will determine which package manager is configured for the current project, install it if not already installed, and finally run it. All users see is the result as if interacting directly with the package manager.

The core of Corepack is:

  • No need to manually install package managers from external installation tools anymore.
  • Ensure that everyone in the team will use the correct version of the package manager through the configuration in "package.json".

How to use

As it is still in the experimental phase, Corepack needs to be activated with the corepack enable command, simply enter in the terminal:

$ corepack enable

Immediately, Corepack will be activated, and you can check the version of yarn or pnpm right now:

$ yarn --version
1.22.19

$ pnpm --version
8.5.1

Currently, Corepack only supports two package managers: yarn and pnpm. If you no longer want to use it, you just need to run the corepack disable command.

$ corepack disable

To determine the package manager used in the project, you can set it through the "packageManager" attribute in package.json. Open package.json and check if the packageManager attribute exists, if not, add it:

{
   ...  
   "packageManager": "yarn",  
   ...  
}

This action means that you configure yarn as the default package manager, if you intentionally use pnpm in the project to install or do anything else, an error message will be displayed on the screen requiring the use of yarn.

This is an exception with npm, meaning you can still use the npm command while "packageManager" is set as yarn or pnpm.

Summary

npm is the default package manager bundled with Node.js. Although it is a powerful tool, npm is not free from flaws. That's why Corepack was born with two package managers, yarn and pnpm. Now there's no need to manually install your favorite package manager anymore, you can use it right in Node.js.

References:

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (0)

Leave a comment...