Using Peer Dependencies With Local (file:../some-lib) Dependencies

独自空忆成欢 提交于 2020-06-12 06:49:12

问题


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.

Consider this repo structure:

  • lib
    • some-library (peerDepends on foo)
      • index.js (requires foo)
      • node_modules will be empty
  • services
    • 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

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

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.

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:

  • Don't use peerDependencies inside the library
  • Install each peer dependency at the root of the project for the sake of local development and testing.

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.


回答1:


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

node --preserve-symlinks index.js 

Here's a link to the docs



来源:https://stackoverflow.com/questions/50807329/using-peer-dependencies-with-local-file-some-lib-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!