Using other package managers with node has become a whole lot easier

anantoghosh

Ananto Ghosh

Posted on March 5, 2022

Using other package managers with node has become a whole lot easier

If you still are not aware, node v16.9.0 and v14.19.0 was launched with corepack https://nodejs.org/api/corepack.html. A developer can use Corepack to define alternative package managers like yarn and pnpm.
Node will then automatically get the required version of the package manger.

Corepack can also be installed as a global package on earlier node versions.

Benefits

Using package managers this way has two main benefits

  1. Easily synchronize package manager and their versions amongst developers. Though yarn 2+ solves this issue in its own way.
  2. No install step required for the package managers.

Enable

It's much easier to understand with an example.

On a machine with node installed

corepack enable
Enter fullscreen mode Exit fullscreen mode

and that is it!. Now the required package manger will be available when executed.

If corepack is not found in your system, then you can install it as a global package

npm i -g corepack
Enter fullscreen mode Exit fullscreen mode

Use

For example, now to create a new project with yarn, in a folder just execute

yarn init -2
Enter fullscreen mode Exit fullscreen mode

Similarly you are free to use pnpm https://pnpm.io/ as well.

Update package.json

A new field in package.json has been introduced to fix a project to a particular package manager version.

{
  name: 'yarn-test',
  packageManager: 'yarn@3.2.0'
}
Enter fullscreen mode Exit fullscreen mode

This ensures that every developer in your team will use the same version of the package manager.

Running the same version

The major work is all done!

Just run your package manager inside your repo and it should run the version defined in your package.json

For example if the package.json had

{
    ...
    "packageManager": "pnpm@6.32.2"
}
Enter fullscreen mode Exit fullscreen mode

Running pnpm in this project will use the same version.

pnpm -v
6.32.2
Enter fullscreen mode Exit fullscreen mode

Conclusions

I wish I had more to say, but corepack makes switching to other package managers a breeze. What qualities distinguish your preferred package manager?

Originally posted on mein.in

💖 💪 🙅 🚩
anantoghosh
Ananto Ghosh

Posted on March 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related