gpt4 book ai didi

node.js - 将对等依赖项与本地依赖项(文件 :. ./some-lib)一起使用

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

我有一个单一存储库,其中包含许多微服务。我希望将一些库类型的函数/类提供给任何需要它的微服务。但是,如果该库包声明了对等依赖项,则当从依赖于该库的事物内运行代码时,不会找到对等依赖项。

考虑这个存储库结构:

    • some-library(peerDepends on foo)
      • index.js(需要 foo)
      • node_modules 将为空
  • 服务
    • some-service(取决于 foosome-library)
      • index.js(需要some-library)
      • node_modules 将具有:
      • foo
      • some-library 将是 ../../lib/some-library
      • 的符号链接(symbolic link)

运行node services/some-service/index.js时,您将收到错误“无法找到模块'foo'”,该错误来自lib/some-library/index .js.

可能会发生这种情况,因为 Node 只查看 lib/some-library/node_modules 以及祖先目录中的任何 node_modules 文件夹。但由于此代码是从 services/some-service (作为工作目录)运行的,并且由于 services/some-service/node_modules 中的符号链接(symbolic link),我会'我预计这会起作用。

这是一个您可以轻松克隆以查看问题的存储库:https://github.com/jthomerson/example-local-dependency-problem

git clone git@github.com:jthomerson/example-local-dependency-problem.git    
cd example-local-dependency-problem
cd services/some-service
npm install
node index.js

我只看到两个解决方案:

  • 不要在库内使用peerDependency
  • 在项目根目录安装每个对等依赖项,以便进行本地开发和测试。

这两个都不是一个真正好的解决方案,因为它不允许每个服务具有不同版本的依赖项,因此意味着如果依赖项的本地版本(或库的版本)发生冲突,则所有服务使用该库然后同时更改其依赖项版本,这使得它们更加脆弱,因为它们都捆绑在一起。

最佳答案

添加 --preserve-symlinks 标志怎么样?例如:

node --preserve-symlinks index.js 

Here's a link to the docs

关于node.js - 将对等依赖项与本地依赖项(文件 :. ./some-lib)一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50807329/

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