workspaces

使用工作区

选择命令行版本:

描述

🌐 Description

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

这套功能使处理来自本地文件系统的关联包的工作流程更加简化。它将链接过程自动化为 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. It automates the linking process as part of npm install and removes the need to manually use npm link in order to add references to packages that should be symlinked into the current node_modules folder.

我们还将这些在 npm install 期间被自动符号链接的包称为单个 工作区,意思是它是当前本地文件系统内的一个嵌套包,并在 package.jsonworkspaces 配置中被明确定义。

🌐 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 配置 添加、移除或更新工作区的依赖。

🌐 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 来使用任何已定义的工作区。从上面定义的示例继续,我们还可以创建一个 Node.js 脚本,该脚本将引入工作区 a 示例模块,例如:

🌐 Given the specifics 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

或者对 'packages' 文件夹中的每个工作区运行该命令:

🌐 Or run the command for each workspace within the 'packages' folder:

npm run test --workspace=packages

也可以使用 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