gpt4 book ai didi

javascript - Lerna bootstrap 不链接本地依赖项?

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:47 28 4
gpt4 key购买 nike

使用 lerna 和本地依赖项的正确方法是什么?

<小时/>

我已经在 mono 存储库中配置了两个模块,以将 lerna 与本地依赖项一起使用。我预料到了

$ lerna bootstrap
$ lerna run test

足以下载所有外部依赖项、链接本地依赖项并执行并通过所有模块中的所有测试。

预期行为

根据 lerna bootstrap文档:

  1. Symlink together all Lerna packages that are dependencies of each other.

因此,我预计 lerna bootstrap 会在 module-b/node_modules 下面创建一个指向 module-a 的符号链接(symbolic link)(其中然后将允许测试执行并通过)。

当前行为

没有发生链接,这会导致测试失败:

lerna ERR! yarn run test exited 1 in 'module-b' lerna ERR! yarn run test stdout: yarn run v1.19.1 $ jest info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

lerna ERR! yarn run test stderr: FAIL ./import.test.js ● Test suite failed to run

Cannot find module 'module-a' from 'import.test.js'

> 1 | const moduleA = require('module-a');
| ^
2 |
3 | test('should import module-a', () => {
4 | moduleA();

at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (import.test.js:1:1)

重现步骤

  1. 创建下面的文件夹结构
  2. 将内联文件复制到相应的目录
  3. 执行 $lerna bootstrap,然后执行 $lerna run test
project-root
+ packages
| + module-a
| | + package.json
| | + index.js
| |
| + module-b
|  | + package.json
| | + import.test.js
|
+ lerna.json

packages/module-a/package.json

{
"name": "module-a",
"version": "1.0.0",
"private": true,
"main": "index.js",
"scripts": {
"test": "echo \"Test passed in module-a\""
}
}

packages/module-a/index.js

module.exports = () => console.log('Log in module-a');

packages/module-b/package.json

{
"name": "module-b",
"version": "1.0.0",
"private": true,
"main": "index.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"module-a": "file:../module-a"
},
"devDependencies": {
"jest": "^24.9.0"
}
}

packages/module-b/import.test.js

const moduleA = require('module-a');

test('should import module-a', () => {
moduleA();
});

lerna.json

{
"npmClient": "yarn",
"packages": [
"packages/*"
],
"version": "independent"
}

观察

执行lerna link --force-local不改变现状,测试仍然失败,因为 module-b/node_modules/ 仍然不包含对 module-a 的引用。

备注

我无法使用yarn workspaces与 lerna 一起,因为 module-b 是一个 Electron 应用程序,并且 Electron 构建器希望将其依赖项安装在 packages/module-b/node_modules/ 文件夹中。

环境

  • lerna --版本 3.18.4
  • npm --版本 6.11.3
  • yarn ——版本1.19.1
  • Node --version v12.12.0
  • macOS Mojave 10.14.6

最佳答案

我已经尝试过你的实现。

packages/module-b/package.json 中使用 file:../module-a 对我来说不起作用。

您可以通过为module-a编写版本号来轻松解决这个问题(这并不重要,因为我们将force-local)

{
"name": "module-b",
"version": "1.0.0",
"private": true,
"main": "index.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"module-a": "1.0.0"
},
"devDependencies": {
"jest": "^24.9.0"
}
}

在项目的根目录:

本地版本的 Bootstrap 和链接:

npx lerna bootstrap --force-local

运行测试:

npx lerna 运行测试

关于javascript - Lerna bootstrap 不链接本地依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58917736/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com