Mono Repo with Lerna
loading...Lerna is a tool for managing JavaScript projects with multiple packages. It optimizes the workflow around managing multi-package repositories with git and npm.
This article will show you how to create a mono repo with Lerna.
You can checkout tutorial video below.
Create a new project
Create a new project with the following command:
mkdir lerna-example
cd lerna-example
npm init -y
Initialize Lerna
Initialize Lerna with the following command:
npx lerna init
This will create a lerna.json
file in the root of the project.
Create a package
Create a new package with the following command:
npx lerna create @lerna-example/package-1 -y
This will create a new folder packages/package-1
with a package.json
file.
Most common commands
Now that we have a mono repo with a package, it helps to manage all npm packages in root package.json
file and node modules in root node_modules
folder. So, if we npm install
a package in root package.json
file, it will be installed in root node_modules
folder and all packages in packages
folder will be able to use it.
To manage the packages, I often use the following commands:
Run certain npm script in all packages (ex: build
)
npx lerna run build
Run npm script in a certain package (ex: build
in package-1
)
npx lerna --scope=@lerna-example/package-1 run build
Publish all packages
To push mono-repository to npm which is the concept of using npm scope. Meaning it is necessary to create a organization under the account. After that, you can publish packages to npm with the following command:
npx lerna publish
The command above exectues actually npx lerna version
and npx lerna publish from-packages
under the hood.
You can also run it seperately.
If you are using a free tier account, you might come across the following error:
? Are you sure you want to publish these packages? Yes
lerna info publish Publishing packages to npm...
lerna ERR! E402 You must sign up for private packages
The reason is npm treats all packages under the same scope as private packages. So, you either need to upgrade your account to a paid tier or you need to set access
to public
like below:
{
"publishConfig": {
"access": "public"
}
}
Congratulations! You have successfully created a mono repo with Lerna. 🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉
If you want to publish packages to npm, you can also checkout my another video tutorial below.