精华 即将被 Node.js 官方抛弃的废宅阿斗 NPM
发布于 3 年前 作者 atian25 8110 次浏览 来自 分享


突然发现在 Node.js 16.9.0 的官方文档里面,多了个 Experimental 的 Corepack

Corepack is an experimental tool to help with managing versions of your package managers. It exposes binary proxies for each supported package manager that, when called, will identify whatever package manager is configured for the current project, transparently install it if needed, and finally run it without requiring explicit user interactions.

看完文档一脸懵逼:这是啥?要干掉 NPM?为啥这么突然?


Corepack is a zero-runtime-dependency Node script that acts as a bridge between Node projects and the package managers they are intended to be used with during development. In practical terms, Corepack will let you use Yarn and pnpm without having to install them - just like what currently happens with npm, which is shipped by Node by default.


简单来说,Corepack 会成为 Node.js 官方的内置 CLI,用来管理『包管理工具(npm、yarn、pnpm、cnpm)』,用户无需手动安装,即『包管理器的管理器』。


先安装 Node.16.9.0 版本,然后在 package.json 中声明对应的包管理工具:

// package.json
  "name": "corepack-test",
  "packageManager": "yarn@2.0.0",
  // 未来可能会改为这个
  "engines": {
    "pm": "yarn@^2.0.0"


# 单应用激活
$ corepack enable

# 用声明的包管理器,会自动下载对应的 yarn,然后再执行
$ yarn install

# 用非声明的包管理器,会自动拦截报错
$ pnpm install
Usage Error: This project is configured to use yarn

我嚓,没全局安装 yarn 也能找到命令,怎么搞的?分析了下,非常粗暴。。。

$ which corepack    

$ ll /Users/tz/.nvs/node/16.9.0/x64/bin/
corepack -> ../lib/node_modules/corepack/dist/corepack.js
npm -> ../lib/node_modules/npm/bin/npm-cli.js
npx -> ../lib/node_modules/npm/bin/npx-cli.js
pnpm -> ../lib/node_modules/corepack/dist/pnpm.js
pnpx -> ../lib/node_modules/corepack/dist/pnpx.js
yarn -> ../lib/node_modules/corepack/dist/yarn.js
yarnpkg -> ../lib/node_modules/corepack/dist/yarnpkg.js


# 全局指令如 npm init 这种,需要设置
$ corepack prepare yarn@x.y.z --activate

# 也支持代理方式,类似 npx 执行远程包
$ corepack yarn@2.1.0 install


  • 用户无需手动安装包管理器,有点类似 npx 执行远程包。
  • 每个应用都可以声明自己的包管理工具以及对应的版本,类似于 cnpm 的 install-node 机制。
  • 目前还在非常早期阶段,Bug 不少,名字也还不统一,有叫 pmm 也叫 corepack。


为什么要做这个? ​

Various problems arise from npm being the only package manager shipped by default:

  • Projects using popular package management solutions other than npm (particularly Yarn and pnpm) require additional installation step that must often be repeated when switching between Node versions. This lead to a significant part of the Node userbase effectively being a second-class citizen, which sounds unfortunate.
  • Because one package manager currently holds a special treatment, users are more likely to pick it even if they would choose another solution should they have the choice (it really depends on how they balance the tradeoffs, but sometimes they value simplicity over purely technical factors). This artificial barrier hurts our community by making it harder to pick the right tool for the job.
  • Having a single official package manager means that all the keys belong to a single player which can do whatever it pleases with it (even the Node project only has a limited influence over it, since removing the unique package manager would be poorly accepted by the community). Spreading these responsibilities over multiple projects gives less power to each, ensuring that everyone behave well.


简单的说,发起者认为,npm 目前是唯一的包管理工具,导致广大开发者喜爱的 pnpm、yarn 等工具成为二等公民,伤害到用户体验以及社区的良性发展。早在 2017 年就发起的讨论,在最近的 TSC 表决中通过。 ​

虽然发起者 arcanis 利益相关,是 yarn 的 Lead maintainer,但只想给它点个赞,并对 npm 说:你也有今天! ​

npm 这个阿斗,在 Node.js 起步时起了很大的作用,但它毕竟是个商业公司,而且这么多年来,一直不思进取:

  • 在 npm 负责人变更后。。。
  • 在 npm 打败 bower 统一前端包管理并承诺会对前端包有更好的支持后。。。
  • 在 yarn 这个鲶鱼出现后。。。
  • 在 GitHub 收购 npm 后。。。

我们一次一次的重燃希望又一次一次的失望,这废宅只会躺平,三天打鱼两天晒网,直到今天,Node.js 官方决定把他赶出门磨练下。



The full npm package wouldn’t be included out of the box anymore (this might be an incremental move, with first a major version shipping pmm + npm, and the next one discarding npm).

  • Corepack 从 16.9.0 开始,内置到 Node.js 的安装包中。
  • npm 将慢慢从 Node.js 内置包中移除,预计在下一个大版本启动。
  • 目前仅支持 pnpm 和 yarn,我们的 cnpm 晚点会提 PR 支持下。
  • 包管理器发布新版本后,需提 PR 到 Corepack 来更新版本。
  • 未看到切换安装源方面的能力,我们将参与进去,把国内的使用场景同步给社区。



7 回复


corepack 名字就不如 npm 顺口和好理解,符合 yarn 这种不接地气的命名风格

@hyj1991 我一直没想通的是,如何称呼?

Node 模块 → 忽略了前端模块 前端模块 → 忽略了 Node npm 模块 → npm 呢?

@atian25 起名也是个技术活,哈哈

看似抹平了各种 *npm *yarn 使用方式,但是目前项目里 package-lock.json 和 yarn.lock 还是二选一,基本不会出现切换使用的场景;不知道能期待点啥…

@DevinXian 并没有到抹平这一步,只是让 npm 不再成为那个唯一官方指定,这是撬动 包管理标准化 的第一步,这一步都没有的话,其他的都免谈了。