如何在Lerna + Yarn Workspaces回购中安装npm软件包?

时间:2020-06-26 20:47:57

标签: npm yarnpkg lerna yarn-workspaces

如果要向程序包添加依赖项,是否需要cd进入该目录并运行yarn add <package>?或者,是否可以在根目录中运行带有--workspace=<workspace-name>之类的标志的命令?

1 个答案:

答案 0 :(得分:1)

您不需要cd进入包目录并运行yarn add <package>。您可以在多包存储库的根路径中使用带有 lerna add 过滤选项的 --scope 命令。

<块引用>

向匹配的包添加单个依赖

例如,将 chalk 依赖项添加到 packages/pkg-a

 [main] ⚡  npx lerna add chalk --scope pkg-a
lerna notice cli v3.22.1
lerna notice filter including "pkg-a"
lerna info filter [ 'pkg-a' ]
lerna info Adding chalk in 1 package
lerna info bootstrap root only
npm WARN @octokit/plugin-request-log@1.0.3 requires a peer of @octokit/core@>=3 but none is installed. You must install peer dependencies yourself.

added 6 packages from 3 contributors and audited 1319 packages in 4.811s
found 0 vulnerabilities

安装后会在chalk文件的dependencies字段中添加packages/pkg-a/package.json

{
  "name": "pkg-a",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node app.js",
    "test": "jest --config jest.config.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chalk": "^4.1.0",
    "express": "^4.17.1"
  }
}

lerna 版本:

[main] ⚡  npx lerna -v     
3.22.1