node-optional icon indicating copy to clipboard operation
node-optional copied to clipboard

Does not always work for monorepository.

Open sasaxing opened this issue 7 years ago • 4 comments

Sometimes if an optional module optModule is installed in one of the sub-packages instead of in projectRoot/node_modules, then it won't be found by optional('optModule').

sasaxing avatar Mar 15 '18 13:03 sasaxing

Any update / alternative on this issue?

BrunnerLivio avatar Oct 02 '18 17:10 BrunnerLivio

@BrunnerLivio do you have an example I can troubleshoot this with?

tony-o avatar Oct 04 '18 17:10 tony-o

There you go: https://github.com/BrunnerLivio/optional-monorepo-bug

When using yarn workspaces, some dependencies might get installed in a subfolder of a package. In this example it would be the dependency packages/core/node_modules/lodash.

Since optional only uses the NodeJS require resolution, which only checks dependencies upwards from the root directoy e.g. rootDirectory/../package.json and not downwards rootDirectory/*/package.json it can not find the lodash package.

├── index.js
├── node_modules
|  └── optional
├── package.json
└── packages
   └── core
      ├── node_modules
      |  └── lodash
      └── package.json

Honestly I do not know how properly fix this bug. One option would be to explicitly support yarn workspaces, therefore parse the package.json of the root directory and search for the workspaces attribute, but I am not quite sure this is the right way to go..

BrunnerLivio avatar Oct 04 '18 17:10 BrunnerLivio

@BrunnerLivio what mechanism do you use outside of optional that searches downwards into packages/?

tony-o avatar Oct 05 '18 15:10 tony-o