Using Corepack
While browsing the metamask-extension source code, I saw a field in package.json: “packageManager”: “yarn@4.9.1”, but when checking yarn on npm, the latest version is still 1.22.22. Strange, so how does the actual project install this v4 yarn? After careful investigation, I found that it uses
corepack
for package manager and version management.
Description
Corepack is an official Node.js project, not third-party. The current latest version is v0.34.0
.
How to use corepack
corepack enable
# Use yarn, pnpm, npm etc. normally for package installation
yarn install
pnpm install
npm install
When executing these package commands, corepack will automatically switch to the specified version. If the package manager or that version of the package manager doesn’t exist, it will automatically install and switch to use it.
Is corepack built-in?
Starting from Node.js v16.9.0, Corepack is a built-in feature of Node.js
Is corepack enabled by default?
Actual testing shows that even Node.js v20 doesn’t have it enabled by default, you still need to manually corepack enable
.
Final Thoughts
I personally think corepack does solve a pain point, because different versions of package managers have different behaviors when installing dependencies, often causing lock file changes, which is super annoying. This way everyone uses unified package managers and versions, so lock file changes become very stable. Of course, if you need to use package managers like yarn outside of projects, you still need to install them globally yourself.