Describe the bug
moleculer (https://www.npmjs.com/package/moleculer) is a microservices framework that uses a "broker" to distribute commands and events and data. When a package depends on a library that includes moleculer and transporter, the transporter module is saved to the cache of the actual package but cannot be used from moleculer. It works with v1, though.
To Reproduce
The repro consists of multiple files. Therefore I created this repro repo: https://github.com/jeffrson/yarn2_moleculer
Instructions are given in the Readme. Essentially you should init yarn2, run yarn and start index.js via node from a package script.
Environment if relevant (please complete the following information):
Additional context
It works with v1
This is most probably related to moleculer loading the module during runtime only when necessary:
https://github.com/moleculerjs/moleculer/blob/master/src/transporters/nats.js#L63
When moleculer and transporter are included in the app itself, ie not in a dependent library, there will be no such problem.
Without checking the repro itself, it seems that the line you linked from the moleculer source tries to require nats without defining it as a peerDependency in https://github.com/moleculerjs/moleculer/blob/master/package.json
This is a problem:
https://yarnpkg.com/advanced/rulebook/#packages-should-only-ever-require-what-they-formally-list-in-their-dependencies
You can work around it by using packageExtensions:
packageExtensions:
moleculer:
peerDependencies:
nats: "*"
Remember to rerun yarn install after updating your packageExtensions.
Here's an example on how this might be solved as a PR to moleculer: https://github.com/nestjs/nest/pull/5477/files
An issue exists in the moleculer repo and a PR is open.
Issue: https://github.com/moleculerjs/moleculer/issues/623
Pull Request: https://github.com/moleculerjs/moleculer/pull/828
In the mean time the packageExtensions feature will work 馃檪
Oh, well, I should have read the whole thread over there...
For the sake of correctness: packageExtension works, but needs something like "moleculer@*", instead of simply "moleculer".
I'll close this then since it has a fix upstream
Most helpful comment
Without checking the repro itself, it seems that the line you linked from the moleculer source tries to
requirenatswithout defining it as apeerDependencyin https://github.com/moleculerjs/moleculer/blob/master/package.jsonThis is a problem:
https://yarnpkg.com/advanced/rulebook/#packages-should-only-ever-require-what-they-formally-list-in-their-dependencies
You can work around it by using packageExtensions:
Remember to rerun
yarn installafter updating yourpackageExtensions.Here's an example on how this might be solved as a PR to moleculer: https://github.com/nestjs/nest/pull/5477/files