- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v16.20.2 文档
- Node.js v16.20.2
- 目录
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
Corepack#>
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.】
此特性简化了两个核心工作流程:
【This feature simplifies two core workflows:】
- 它简化了新贡献者的入职流程,因为他们不再需要仅仅为了使用你希望他们使用的包管理器而遵循特定系统的安装步骤。
- 它允许你确保团队中的每个人都将使用你希望他们使用的确切包管理器版本,而无需他们在每次你需要进行更新时手动同步。
工作流程#>
【Workflows】
启用该功能#>
【Enabling the feature】
由于其实验性状态,目前需要显式启用 Corepack 才能生效。要做到这一点,请运行 corepack enable,这将会在你的环境中 node 二进制文件旁设置符号链接(如有必要,也会覆盖现有的符号链接)。
【Due to its experimental status, Corepack currently needs to be explicitly
enabled to have any effect. To do that, run corepack enable, which
will set up the symlinks in your environment next to the node binary
(and overwrite the existing symlinks if necessary).】
从现在起,对 支持的二进制文件 的任何调用都将正常工作,无需进一步设置。如果你遇到问题,请运行 corepack disable 从系统中移除代理(并考虑在 Corepack 仓库 上提交问题以告知我们)。
【From this point forward, any call to the supported binaries will work
without further setup. Should you experience a problem, run
corepack disable to remove the proxies from your system (and consider
opening an issue on the Corepack repository to let us know).】
配置包#>
【Configuring a package】
Corepack 代理将会在你当前的目录层级中找到最接近的 package.json 文件,以提取其 "packageManager" 属性。
【The Corepack proxies will find the closest package.json file in your
current directory hierarchy to extract its "packageManager" property.】
如果该值对应 支持的包管理器,Corepack 将确保对相关二进制文件的所有调用都运行在所请求的版本上,如有需要会按需下载,并在无法成功获取时中止。
【If the value corresponds to a supported package manager, Corepack will make sure that all calls to the relevant binaries are run against the requested version, downloading it on demand if needed, and aborting if it cannot be successfully retrieved.】
升级全局版本#>
【Upgrading the global versions】
在现有项目之外运行时(例如运行 yarn init),Corepack 默认会使用预定义版本,这些版本大致对应每个工具的最新稳定版本。可以通过运行 corepack prepare 命令并指定你希望设置的包管理器版本来覆盖这些版本:
【When running outside of an existing project (for example when running
yarn init), Corepack will by default use predefined versions roughly
corresponding to the latest stable releases from each tool. Those versions can
be overridden by running the corepack prepare command along with the
package manager version you wish to set:】
corepack prepare yarn@x.y.z --activate
离线工作流程#>
【Offline workflow】
许多生产环境没有网络访问权限。由于 Corepack 通常直接从其注册表下载包管理器版本,这可能会与此类环境发生冲突。为了避免这种情况,请在仍然可以访问网络时(通常是在准备部署镜像时)运行 corepack prepare 命令。这将确保即使没有网络访问,也能使用所需的包管理器。
【Many production environments don't have network access. Since Corepack
usually downloads the package manager releases straight from their registries,
it can conflict with such environments. To avoid that happening, call the
corepack prepare command while you still have network access (typically at
the same time you're preparing your deploy image). This will ensure that the
required package managers are available even without network access.】
prepare 命令具有 各种标志。有关更多信息,请查阅详细 Corepack 文档。
【The prepare command has various flags. Consult the detailed
Corepack documentation for more information.】
支持的包管理器#>
【Supported package managers】
以下二进制文件通过 Corepack 提供:
【The following binaries are provided through Corepack:】
| 包管理器 | 二进制名称 |
|---|---|
| Yarn | yarn, yarnpkg |
| pnpm | pnpm, pnpx |
常见问题#>
【Common questions】
Corepack 如何与 npm 交互?#>
【How does Corepack interact with npm?】
虽然 Corepack 可以像其他包管理器一样支持 npm,但它的 shim 默认情况下并未启用。这会带来一些后果:
【While Corepack could support npm like any other package manager, its shims aren't enabled by default. This has a few consequences:】
- 在一个配置为使用其他包管理器的项目中运行
npm命令始终是可能的,因为 Corepack 无法拦截它。 - 虽然
npm是"packageManager"属性中的一个有效选项,但缺少 shim 会导致使用全局 npm。
运行 npm install -g yarn 无效#>
【Running npm install -g yarn doesn't work】
npm 在进行全局安装时会防止意外覆盖 Corepack 二进制文件。为避免此问题,可以考虑以下几种方法:
【npm prevents accidentally overriding the Corepack binaries when doing a global install. To avoid this problem, consider one of the following options:】
- 不要运行这个命令;Corepack 无论如何都会提供包管理器的二进制文件,并确保所请求的版本始终可用,因此无需显式安装包管理器。
- 在
npm install中添加--force标志;这会告诉 npm 可以覆盖二进制文件,但在此过程中你会删除 Corepack 的二进制文件。(运行corepack enable可将它们添加回来。)