API with NestJS #59. Introduction to a monorepo with Lerna and Yarn workspaces

JavaScript NestJS

This entry is part 59 of 121 in the API with NestJS

Monorepo is an approach in which we store multiple projects in the same repository. It is common across big tech companies, such as Uber or Google. Even NestJS manages its source code in a monorepo with Lerna.

When we store all of our code in a single repository, it might be easier to understand how different parts of our systems relate to each other. Also, having all of our applications in one place can endorse sharing the code. Good examples are models and utility functions.

While the monorepos have advantages, we must avoid making our codebase messy. This article uses Lerna to manage our monorepo while creating a NestJS project. You can view all of the code from this article in this repository.

Starting a project with Lerna

Let’s kickstart our project with Lerna:

Running does a few things for us:

  • creates a new project with containing in ,
  • defines a file with a basic configuration,
  • adds a directory where, by default, our apps go.

Let’s look at the file created by the above command.

lerna.json

With the array, we can define directories where we want to define our applications. The string is the current version of our repository.

Two approaches to versioning

With Lerna, we can choose one of the two approaches to versioning.

Fixed mode

By default, Lerna uses the fixed mode and uses a single version for the whole repository. Doing that binds the versions of all our packages. When changing the code one package and publishing a new version, we bump the version in all our packages. It is a straightforward approach but might result in unnecessary version changes for some of our packages. Since this is the default versioning mode, we use it in this article.

Independent mode

Lerna allows us to specify package versions separately for every package in the independent mode. Every time we publish, we need to establish a new version of each package that changed. Also, when using the independent mode, we need to specify in our .

Preparing the environment

In our case, we want to separately store applications such as microservices and libraries, such as reusable utilities. To do that, we need to modify our .

lerna.json

Yarn workspaces

The workspaces feature built into Yarn works by optimizing the installation of dependencies for multiple projects at once. Every project that we create in our monorepo is a separate workspace. The dependencies across workspaces are linked together and depend on one another. The above feature is very commonly used with Lerna.

lerna.json

Defining ESLint and TypeScript configuration

Before defining our first package, let’s define a TypeScript and ESlint configuration that we will use in every project we have.

tsconfig.json

tsconfig.build.json

.eslintrc.json

.prettierrc

jest.config.js

In all of the above configuration files, I’m mostly using the default values created by NestJS CLI.

We can put the dependencies that we want to use in every package into the global :

package.json

Make sure to also add .

Creating our first packages

Thanks to defining the global configuration in the above way, we can now define our first package that uses the global configuration.

applications/core/tsconfig.json

applications/core/tsconfig.build.json

applications/core/.eslintrc.js

applications/core/jest.config.js

applications/core/package.json

Because the above dependencies are defined in the file and not in the global , they will be installed just for the application.

Adding multiple packages

We’ve created a simple microservice in part #18 of this series. Let’s define the application in a similar way to the app.

The crucial logic that we aim to use is in the file.

For a full example visit this repository.

applications/email-subscriptions/src/subscribers/subscribers.controller.ts

Thanks to doing the above, we can use our microservice in the application.

applications/core/src/email-subscriptions/emailSubscriptions.controller.ts

After defining both and applications, our current file structure looks like that:

Installing all dependencies and running scripts

Even though we have more than one in our project, we can install our dependencies with one command using the command. Doing the above creates three directories for us. One of them is at the root of our project, and the other is for the and packages.

Once we install all of our dependencies, we can run to run a particular script in all packages. For example, running runs ESLint in both and packages.

Summary

In this article, we’ve gone through the basics of using NestJS with Lerna. We’ve learned about different approaches to versioning we can implement and how to add Yarn workspaces to our configuration. We’ve also created reusable TypeScript, ESLint, and Jest configurations. On top of that, we’ve built a project that defines two applications. Within them, it establishes a connection to a microservice.

There is much more we can write on the topic of managing monorepos with Lerna, so stay tuned!

Series Navigation<< API with NestJS #58. Using ETag to implement cache and save bandwidthAPI with NestJS #60. The OpenAPI specification and Swagger >>
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Gathsara
Gathsara
2 years ago

Thank you for your hard work… Need more of nest js. I learn everything from you, sir.

nguyen van kien
nguyen van kien
1 year ago

you can rcm for me several documents about monorepo nesjts ?
i thank you very much.