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:
foo
)
foo
)foo
, and some-library
)
some-library
)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 [email protected]: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:
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.
How about adding the --preserve-symlinks
flag?
E.g.:
node --preserve-symlinks index.js
Here's a link to the docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With