Why are unmet peer dependencies not resolved when using yarn link?

410 views Asked by At

I have 2 node packages : foo-service and foo-commons.

foo-commons is a dependency of foo-service.

While development,every time there is a change in foo-commons, to use them in foo-service I copy the compiled source from foo-commons to foo-service/node_modules/foo-commons/:

cp -r ./foo-commons/dist ./foo-service/node_modules/foo-commons/dist

I recently came across a better way, which is using yarn link. link

yarn link creates a sylink from ./foo-service/node_modules/foo-commons to ./foo-commons/

With this, the typescript build succeeds without manually copying the files like before.

However, when I run foo-service, I get a module not found error :

Error: Cannot find module 'mongoose'
/foo-service/dist/server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
at Function.Module._load (internal/modules/cjs/loader.js:526:27)
at Module.require (internal/modules/cjs/loader.js:666:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (/foo-commons/node_modules/@typegoose/typegoose/lib/typegoose.js:6:18)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Module.require (internal/modules/cjs/loader.js:666:19)

This error occurs only when I use yarn link.

The module in the error mongoose is an unresolved peer dependency for foo-commons:

warning " > @typegoose/[email protected]" has unmet peer dependency "mongoose@^5.9.22".

Why are unmet peer dependencies not resolved when using yarn link?

1

There are 1 answers

0
Aleksander Burzec On

I made a workaround to this issue.

First, in project, whicch uses my library I installed npm-install-linked Then I made a custom script to install:

  1. project tree:
my_lib:
-- peerDependencies:
-----a
-----b

my_project:
----package.json:
--------file:../../my_lib_/dist/my_lib
  1. Using npm-install-linked I install peer dependency in <my_library>/node_modules (including peer depepndencies):
my_project:
---node_modules
------my_lib:
---------node_modules:
------------a
------------b

  1. I'm copying this node_modules to my_project root node_modules, skipping existing libraries:
cp -R -n node_modules/my_lib/node_modules/* node_modules/
  1. And deleting node_modules of library to avoid build errors:
rm -rf node_modules/my_lib/node_modules

After this operations project tree:

my_project:
---node_modules:
-----a
-----b
-----my_lib

This is dirty workaround, but I didn't manage to do it better. If you know, how to do it better, let me know!