15

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)

1

2 Answers 2

11

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.

2
  • You say "you would need to deploy or work with the whole mono-repo", but I think the recommended approach (see github.com/lerna/lerna/blob/master/doc/hoist.md "Forgotten Dependencies") is to also include the hoisted dependencies in the package that needs it, as well as the monorepo root. In fact they appear to advise linting to enforce that. Then, run from the monorepo every package gets the exact same dep, but the packages can also work independently. Commented Oct 18, 2019 at 10:28
  • @user568458 Sounds inefficient to me including deps that aren't needed in the leaf published NPM tarballs. Commented Oct 18, 2019 at 12:56
3

If your packages are npm packages that you intend to reuse by publishing to some npm registry, then you should give them proper package.json dependencies.

A package should always list what it requires to operate. E.g. if your "api" package requires lodash at runtime, then it MUST have lodash in its dependencies. Otherwise an app could install it without lodash; then it would fail to run.

In your lerna repo, your "root" package.json is not connected to any npm package, and is not published, so it doesn't affect your "real" npm packages: "api" and "web-app".

Finally, if you don't intend to publish your packages as npm packages, then do whatever you want. Lerna is probably overkill in that case. Even the package.json is overkill, because it's just being used for its scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.