Table of Content

Summary

Lerna is powerful package which allow develop multiple module in one repository, this approach named monorepo. There are many cool things which can do Lerna, but in this post I describe only minimal usage.

I use Packagist for PHP packages and it has great feature where I can mark module as dev-master in composer.json and Composer will upload code directly from Github with .git directory, but unfortunately npm does not have this feature. That is why I started to use Lerna for NodeJS packages. Now I can develop absolutely independent packages in one project and Lerna help me to organize it.

Getting started

lerna is a CLI package. You’re going to want to install it with the --global (-g) flag.

$ npm i lerna -g

Now, I’m going to create new directory where independent packages will be placed.

$ mkdir my-project
$ cd my-project
$ lerna init --independent

This will do a couple of things:

  • Creating packages folder.
  • Updating package.json.
  • Creating lerna.json.

The packages/ directory is where all your packages belong. Let’s go about cloning your package from Github.

$ cd packages/
$ git clone https://github/popovserhii/scrapoo

Now go about creating usage/. You can imagine it as your project where another packages will be used.

$ mkdir usage
$ cd usage
$ npm init -y
$ touch index.js

What you want to do now is go into packages/usage/package.json and add these lines under dependencies.

{
  "dependencies": {
    "scrapoo": "0.1.0",
  }
}

Here is important moment you must take real package version from packages/scrapoo/package.json and append it to a package which relies on your package.

Now you need to wire everything up with this command. Go to root directory of my-project and run

$ lerna bootstrap

The output from this command should look something like this:

Lerna v3.1.0
Linking all dependencies
Successfully bootstrapped 2 packages.

After that scrapoo will be append as dependency to node_packages of usage/ and it will be linked with scrapoo from packages/ directory. You can develop packages independent, make commits and push to Github and in the same time use this packages together. You can add any number of packages to packages/ and use them as dependencies in another packages and Lerna help you with correct linking between them.

Useful links

Last modified: 30.05.2019

Author

Comments

Write a Reply or Comment

Your email address will not be published.