DEV Community

Victor Magarlamov
Victor Magarlamov

Posted on

2 2

Monorepo App with Lerna

Monorepo is a way to organize an application. In this case, the application is divided into several parts, each of which is a separate package. For example, look at the React repository. You will see that the main parts of this library are separate packages: "react-reconciler", "react-dom"… In other word, monorepo is a multi-package repository.

What are the advantages of this way? In a nutshell, separation into logical parts facilitates versioned, testing and understanding of the overall project. But better than any words is practical experience.

Many years ago I made a personal website for the russian artist Gregory Maiofis. It was a monolithic Ruby On Rails application. Recently I decided to rewrite this as a single-page and monorepo application. You can see the result on github.

In this work I used Lerna and below I want to tell you (in short) how to use this multi-package application management tool.

The first command is lerna init. It creates a new lerna repo. Open the lerna.json file after executing this and add the following lines:

"useWorkspaces": true, 
"command": {
  "run": {
    "npmClient": "yarn"
  }
}

And add to the package.json file:

"workspaces": {
  "packages": [
    "packages/*"
  ]
}

These changes allow us to use yarn workspaces.

I organized my project in the following manner:

packages
  |---app   // main application with CRA
  |---admin // Admin Panel
  |---ui    // a library of common React components
  |---api   // a library for working with API

The main commands:

// bootstrap the packages in the current Lerna repo
lerna bootstrap

// Symlink together all packages
lerna link

// execute the "start" command in all packages 
// that contains this command
// in my case it will be "app" and "admin"
lerna run start

// execute the build command in the "ui" package only
lerna run --scope @project/ui build

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (4)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

OK, just don't Lerna to mix frontend and backend. They needs different kinds of node_modules.

Collapse
 
victormagarlamov profile image

Yes, I understand it. In my project the api package is a set of methods that send requests to backend. But anyway thanks for your comment)

Backend and frontend in one is a monolithic app.

Collapse
 
zkochan profile image
Zoltan Kochan

I'd recommend to use pnpm + changesets or Rush instead of Lerna.

Collapse
 
victormagarlamov profile image
Victor Magarlamov

Thank you for these links! I didn't know about these projects.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay