NPM 即将被 Node.js 官方抛弃

点击上方“蓝色字体”,选择“设为星标

做积极向上的前端人!

来源:https://zhuanlan.zhihu.com/p/408122100

楔子

突然发现在 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.

译文:

Corepack 是一个处于实验阶段的工具,用于助你管理你本机 package manager 的版本。它为每个包管理工具都暴露了二进制代理,当 Corepack 被调用时,将读取当前项目配置的包管理工具,如未安装,则会自动安装它,最后运行它时则不需要用户额外操作。

看完一脸懵逼:这是啥?要干掉 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.

摘自:https://github.com/nodejs/corepack/blob/main/DESIGN.md

译文:

Corepack 是一款零运行时依赖的 Node 脚本工具,它充当 Node 项目和包管理工具之间的桥梁。实际上,Corepack 可以让你直接使用 yarn 和 pnpm,而无需安装它们。就像使用 npm 一样,默认由 Node 提供。

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


初体验

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

// package.json
{
  "name": "corepack-test",
  "packageManager": "yarn@2.0.0"
}

玩起来:

# 单应用激活
$ corepack enable

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

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

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

$ which corepack    
/Users/tz/.nvs/node/16.9.0/x64/bin/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.

摘自:https://github.com/nodejs/corepack/blob/main/DESIGN.md

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

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

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

  • 在 npm 负责人变更后。。。

  • 在 npm 打败 bower 统一前端包管理并承诺会对前端包有更好的支持后。。。

  • 在 yarn 这个鲶鱼出现后。。。

  • 在 GitHub 收购 npm 后。。。

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

PS:需注意的是,这次动的只是 Node.js 安装包的 CLI,而包管理服务(即 npm registry)是没有变化的,大家平时用 yarn 和 pnpm 也是连的 npm registry 或者 cnpm registry 这个国内同步源。


后续规划

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).
摘自:https://github.com/nodejs/corepack/blob/main/DESIGN.md

  • Corepack 从 16.9.0 开始,内置到 Node.js 的安装包中。

  • npm 将慢慢从 Node.js 内置包中移除,预计在下一个大版本启动。

  • 目前仅支持 pnpm 和 yarn,我们的 cnpm 晚点会提 PR 支持下。

  • 包管理器发布新版本后,需提 PR 到 Corepack 来更新版本。

  • 未看到切换安装源方面的能力,我们将参与进去,把国内的使用场景同步给社区。


相关资料

  • Corepack 官方文档:https://github.com/nodejs/corepack/blob/main/DESIGN.md

  • Node.js 官方文档:https://nodejs.org/dist/latest-v16.x/docs/api/corepack.html

  • Node.js Issue 讨论:https://github.com/nodejs/node/issues/15244

  • Node.js TSC 讨论:Vote on integration of Yarn. · Issue #1012 · nodejs/TSC

  • Reddit 讨论:https://www.reddit.com/r/javascript/comments/pjkdms/corepack_experimental_new_nodejs_feature_to/

d3d07a6f725cad664a17a621bc269c3b.png

— END —

45253f1862fdd9afd706c71fb73a00da.gif

点击关注下方卡片关注我👇👇

ac83571b4dbc7d61dc39e899785a4024.gif

前 Twitter CEO 炮轰 Web 3,马斯克也来“掺和一脚”?

Node.js 在 2020 年有什么新东西

如何分析 Node.js 中的内存泄漏?

漫画 | 公司测试因提Bug不规范,锒铛入狱~

漫画 | 为什么月薪过万的互联网人脱单这么难?

漫画 | 渣男,跟你的代码过去吧…

e58234b6779fbea1df71d69d07179aa4.png

如果觉得这篇文章还不错,来个【分享、点赞、在看】三连吧,让更多的人也看到~


aa658354017ce3319f7b287f13289664.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值