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 forfoo-commons
:
warning " > @typegoose/[email protected]" has unmet peer dependency "mongoose@^5.9.22".
Why are unmet peer dependencies not resolved when using yarn link
?
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:npm-install-linked
I install peer dependency in<my_library>/node_modules
(including peer depepndencies):node_modules
to my_project rootnode_modules
, skipping existing libraries:node_modules
of library to avoid build errors:After this operations project tree:
This is dirty workaround, but I didn't manage to do it better. If you know, how to do it better, let me know!