node-optional
node-optional copied to clipboard
Does not always work for monorepository.
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').
Any update / alternative on this issue?
@BrunnerLivio do you have an example I can troubleshoot this with?
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 what mechanism do you use outside of optional that searches downwards into packages/?