Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lerna. Install dependencies to root project

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)

like image 478
WelcomeTo Avatar asked Dec 12 '18 08:12

WelcomeTo


People also ask

How do I add dependencies to Lerna?

Install Dependency to Specific Package With LernaLerna comes with an add command to install NPM dependencies in your project's packages. By default, lerna adds a new dependency to all managed packages. That's it! You can now use the added dependency in the related package.

What is NPX lerna?

Lerna is a fast modern build system for managing and publishing multiple JavaScript/TypeScript packages from the same repository. Get Started. $ npx lerna init. Thousands of teams worldwide are using Lerna.

What does NPX lerna bootstrap do?

Lerna's bootstrap command installs package dependencies and links the packages together. Without bootstrapping your packages, the package dependencies will not satisfy, and you won't be able to run any npm scripts across the project.


1 Answers

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.

like image 65
Matthias Avatar answered Sep 19 '22 02:09

Matthias