将 Peer 依赖与本地 (file:../some-lib) 依赖一起使用 [英] Using Peer Dependencies With Local (file:../some-lib) Dependencies

查看:46
本文介绍了将 Peer 依赖与本地 (file:../some-lib) 依赖一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多微服务的 monorepo.我想让任何需要它的微服务都可以使用一些库类型的函数/类.但是,如果该库包声明了对等依赖项,则在从依赖于该库的事物中运行代码时将找不到对等依赖项.

I have a monorepo that has many micro-services in it. There are some library-type functions / classes that I want to make available to any micro-service that needs it. However, if that library package declares a peer dependency, the peer dependency is not found when running code from within the thing that depends on the library.

考虑这个回购结构:

    • 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
        • 的符号链接
        • some-service (depends on foo, and some-library)
          • index.js (requires some-library)
          • node_modules will have:
          • foo
          • some-library will be a symlink to ../../lib/some-library

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

          When running node services/some-service/index.js, you'll get the error "Cannot find module 'foo'", emanating from lib/some-library/index.js.

          可能发生这种情况是因为 node 只查看 lib/some-library/node_modules 和祖先目录中的任何 node_modules 文件夹.但是由于此代码是从 services/some-service(作为工作目录)运行的,并且由于 services/some-service/node_modules 中的符号链接,我会我希望这会奏效.

          Presumably this happens because node is only looking at lib/some-library/node_modules and any node_modules folder that is in an ancestor directory. But since this code was run from services/some-service (as the working directory), and because of the symlink in services/some-service/node_modules, I would've expected this to work.

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

          Here's a repo you can easily clone to see the problem: 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    
          

          我只看到两种解决方案:

          I only see two solutions:

          • 不要在库中使用 peerDependencies
          • 为了本地开发和测试,在项目的根目录安装每个对等依赖项.

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

          Neither of those is a real great solution because it doesn't allow each service to have different versions of the dependencies, and thus means that if the local version (or the library's version) of a dependency is bumped, all services that uses the library then have their dependencies version bumped at the same time, which makes them more brittle because they're all tied together.

          推荐答案

          如何添加 --preserve-symlinks 标志?例如:

          How about adding the --preserve-symlinks flag? E.g.:

          node --preserve-symlinks index.js 
          

          这是文档的链接

          这篇关于将 Peer 依赖与本地 (file:../some-lib) 依赖一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆