Monorepo Tutorial using Lerna
YouTube Video
Repositories uses Lerna
Repository commands
npm run clean
: Deletedist
andnode_modules
directoriesnpm run build
: Runwebpack
and build the packagesnpm run link
: Link node_modules using lernanpm run publish
: Publish packagesnpm run changed
: Get changed packages from the last publishnpm run show-dependency
: Get dependencies of packagesnpm run create-version
: Create a new version using lernanpm run registery:serve
: Run verdaccio registry local server
Summary & Ideas
Monorepo is a repository configuration to have different parts of your application microservices under a single respository to improve productivity. If you have multiple packages or components that depend on each other, they usually have similar scripts, commands, dependencies, then having a single repository with everything will reduce the number of duplications, make it easy to access and update different modules and publish different versions at once. It will dramatically increase your team's productivity.
Lerna is used to handle monorepo management. There are many other tools that we will try to discuss.
Some extras regarding Monorepo configurations not mentioned in the YouTube video:
Linking can be done using npm workspaces mode by setting the main package.json file
private: true
and adding theworkspaces
to an array of available packages. You can use a wildcardpackages/*
to apply for all.Typescript: We need to create three tsconfig files; one which will be the main tsconfig file with all needed configs, the second one to import these configs inside each project and a third one is used to reference and build all projects with a single
tsc --build
. First, create the tsconfig at the top level, and then inside each project use the propertyextends
and then pass a reference to the main JSON file with the full typescript configs needed. The build tsconfig file would be a separate file that has two properties; the propertyreferences
set to point to every project andfiles: []
which is an empty array to avoid building the project twice.Babel: You can create a main .babelrc file and then reference it inside each projects’ .babelrc with the property
extends: <path>
ESLint: Same like Babel we create
.eslintrc
file but on the root directory as IDEs tend to prefer them on the root to enforce the rules and then for each project you can create an internal config.eslintrc
file and use theextend
property.Scripty: Tool to manage scripts inside package.json and unify all of them in shell file instead of having a long command in package.json file.
commitlint: In a Monorepo where many teams work together, it’s a great idea to unify how commits are made and their format and also supports automatic changelog. Using Lerna with commitlint and husky will do the required things and setup is very easy. Follow this link.