workspaces

使用工作区

选择命令行版本:

描述

¥Description

工作区是一个通用术语,指的是 npm cli 中的一组功能,它支持在单个顶层根包中管理本地文件系统中的多个包。

¥Workspaces is a generic term that refers to the set of features in the npm cli that provides support to managing multiple packages from your local file system from within a singular top-level, root package.

这组功能弥补了从本地文件系统处理链接包的更加简化的工作流程。自动链接过程作为 npm install 的一部分,避免手动使用 npm link 来添加对应该符号链接到当前 node_modules 文件夹中的包的引用。

¥This set of features makes up for a much more streamlined workflow handling linked packages from the local file system. Automating the linking process as part of npm install and avoiding manually having to use npm link in order to add references to packages that should be symlinked into the current node_modules folder.

我们还将 npm install 期间自动符号链接的这些包称为单个工作区,这意味着它是在 package.json workspaces 配置中显式定义的当前本地文件系统中的嵌套包。

¥We also refer to these packages being auto-symlinked during npm install as a single workspace, meaning it's a nested package within the current local file system that is explicitly defined in the package.json workspaces configuration.

定义工作区

¥Defining workspaces

工作区通常通过 package.json 文件的 workspaces 属性定义,例如:

¥Workspaces are usually defined via the workspaces property of the package.json file, e.g:

{
"name": "my-workspaces-powered-project",
"workspaces": ["packages/a"]
}

鉴于上述 package.json 示例位于当前工作目录 . 中,该目录包含一个名为 packages/a 的文件夹,该文件夹本身包含一个 package.json,定义了一个 Node.js 包,例如:

¥Given the above package.json example living at a current working directory . that contains a folder named packages/a that itself contains a package.json inside it, defining a Node.js package, e.g:

.
+-- package.json
`-- packages
+-- a
| `-- package.json

在当前工作目录 . 中运行 npm install 后,预期的结果是文件夹 packages/a 将符号链接到当前工作目录的 node_modules 文件夹。

¥The expected result once running npm install in this current working directory . is that the folder packages/a will get symlinked to the node_modules folder of the current working dir.

下面是一个帖子 npm install 示例,假设文件和文件夹的先前示例结构相同:

¥Below is a post npm install example, given that same previous example structure of files and folders:

.
+-- node_modules
| `-- a -> ../packages/a
+-- package-lock.json
+-- package.json
`-- packages
+-- a
| `-- package.json

工作区入门

¥Getting started with workspaces

你可以使用 npm init 自动执行定义新工作区所需的步骤。例如,在已经定义了 package.json 的项目中,你可以运行:

¥You may automate the required steps to define a new workspace using npm init. For example in a project that already has a package.json defined you can run:

npm init -w ./packages/a

此命令将创建丢失的文件夹和新的 package.json 文件(如果需要),同时确保正确配置根项目 package.json"workspaces" 属性。

¥This command will create the missing folders and a new package.json file (if needed) while also making sure to properly configure the "workspaces" property of your root project package.json.

将依赖添加到工作区

¥Adding dependencies to a workspace

可以使用 workspace 配置.0 直接添加/删除/更新工作区的依赖。

¥It's possible to directly add/remove/update dependencies of your workspaces using the workspace config.

例如,假设以下结构:

¥For example, assuming the following structure:

.
+-- package.json
`-- packages
+-- a
| `-- package.json
`-- b
`-- package.json

如果你想从注册表添加名为 abbrev 的依赖作为工作区 a 的依赖,你可以使用工作区配置告诉 npm 安装程序应将包添加为所提供工作区的依赖:

¥If you want to add a dependency named abbrev from the registry as a dependency of your workspace a, you may use the workspace config to tell the npm installer that package should be added as a dependency of the provided workspace:

npm install abbrev -w a

注意:uninstallci 等其他安装命令也将遵循提供的 workspace 配置。

¥Note: other installing commands such as uninstall, ci, etc will also respect the provided workspace configuration.

使用工作区

¥Using workspaces

给定 Node.js 如何处理模块解析的细节,可以通过其声明的 package.json name 使用任何定义的工作区。继续上面定义的示例,让我们还创建一个需要工作区 a 示例模块的 Node.js 脚本,例如:

¥Given the specifities of how Node.js handles module resolution it's possible to consume any defined workspace by its declared package.json name. Continuing from the example defined above, let's also create a Node.js script that will require the workspace a example module, e.g:

// ./packages/a/index.js
module.exports = 'a'
// ./lib/index.js
const moduleA = require('a')
console.log(moduleA) // -> a

运行时:

¥When running it with:

node lib/index.js

这演示了 node_modules 解析的本质如何允许工作区启用可移植的工作流程,以便要求每个工作区以同样易于 发布 这些嵌套工作区在其他地方使用的方式。

¥This demonstrates how the nature of node_modules resolution allows for workspaces to enable a portable workflow for requiring each workspace in such a way that is also easy to publish these nested workspaces to be consumed elsewhere.

在工作区上下文中运行命令

¥Running commands in the context of workspaces

你可以使用 workspace 配置选项在已配置工作区的上下文中运行命令。此外,如果你的当前目录位于工作区中,则会隐式设置 workspace 配置,并将 prefix 设置为根工作区。

¥You can use the workspace configuration option to run commands in the context of a configured workspace. Additionally, if your current directory is in a workspace, the workspace configuration is implicitly set, and prefix is set to the root workspace.

下面是一个关于如何在嵌套工作区的上下文中使用 npm run 命令的快速示例。对于包含多个工作区的项目,例如:

¥Following is a quick example on how to use the npm run command in the context of nested workspaces. For a project containing multiple workspaces, e.g:

.
+-- package.json
`-- packages
+-- a
| `-- package.json
`-- b
`-- package.json

通过使用 workspace 选项运行命令,可以在该特定工作区的上下文中运行给定的命令。例如:

¥By running a command using the workspace option, it's possible to run the given command in the context of that specific workspace. e.g:

npm run test --workspace=a

你还可以在工作区中运行该命令。

¥You could also run the command within the workspace.

cd packages/a && npm run test

两者都将运行在 ./packages/a/package.json 文件中定义的 test 脚本。

¥Either will run the test script defined within the ./packages/a/package.json file.

请注意,你还可以在命令行中多次指定此参数以针对多个工作区,例如:

¥Please note that you can also specify this argument multiple times in the command-line in order to target multiple workspaces, e.g:

npm run test --workspace=a --workspace=b

还可以使用 workspaces(复数)配置选项来启用相同的行为,但在所有配置的工作区的上下文中运行该命令。例如:

¥It's also possible to use the workspaces (plural) configuration option to enable the same behavior but running that command in the context of all configured workspaces. e.g:

npm run test --workspaces

将在 ./packages/a./packages/b 中运行 test 脚本。

¥Will run the test script in both ./packages/a and ./packages/b.

命令将按照它们在 package.json 中出现的顺序在每个工作区中运行

¥Commands will be run in each workspace in the order they appear in your package.json

{
"workspaces": [ "packages/a", "packages/b" ]
}

运行顺序与以下不同:

¥Order of run is different with:

{
"workspaces": [ "packages/b", "packages/a" ]
}

忽略丢失的脚本

¥Ignoring missing scripts

并非所有工作区都需要实现使用 npm run 命令运行的脚本。

¥It is not required for all of the workspaces to implement scripts run with the npm run command.

通过运行带有 --if-present 标志的命令,npm 将忽略缺少目标脚本的工作区。

¥By running the command with the --if-present flag, npm will ignore workspaces missing target script.

npm run test --workspaces --if-present

也可以看看

¥See also

npm 中文网 - 粤ICP备13048890号