I have standard Lerna
repository like this:
my-repo
- package.json
- packages
- api
- package.json
- web-app
- package.json
If I need same dependency in both packages (for example lodash
), then people in tutorials suggest to install it to both sub modules and then bootstrap project with with lerna bootstrap --hoist
flag.
Because of --hoist
flag lodash
dependency will be loaded only to root level node_modules
but both sub-modules will contain it as dependency in their appropriate package.json
But Node’s package resolution algorithm searches up the file tree looking for node_modules
folder.
So my question is why I can't just install common dependencies to root level project? Then lodash
will be located under root's node_modules
. And sub-modules (packages) will find it because Node will search up for node_module
until the root of the file system is reached.
At least it will help me to avoid using uncommon lerna bootstrap --hoist
, as well as lodash
dependency will be present only once at the top level package.json
(and not twice: in package.json
of both submodules)
So my question is why I can't just install common dependencies to root level project?
You can, and you're right, node's resolution algorithm will find the shared dependency fine. The downside of doing this is that you lose flexibility, and you would need to deploy or work with the whole mono-repo which might be fine for you. A more traditional approach is to keep production dependencies in the sub packages so that you can publish the packages and use them individually without depending on the root of the monorepo, but again, this might not be important for you.