5 Tips about lerna
In this article, I will introduce some common questions about using lerna to create a javascript monorepos project.
When writing this article, the version of lerna is
3.6.0
If you want to have an independent workspace to play lerna, I will recommend to visit lerna-playbook which is a docker environment for lerna I created before. In addition, you can also read this post as well.
How to get started?
When you clone the project first time, you have to install all the dependencies and link any cross-dependencies. Do not execute npm install
in each packages when initialize, lerna bootstrap
will help you.
You can consider to add lerna bootstrap
in postinstall
command, that will be helpful in some cases, for instances: https://github.com/react-bootstrap-table/react-bootstrap-table2/blob/master/package.json#L8
If you project is not initialized yet, lerna init
can help you to create new lerna project!!
If you project is initialized, but you have create a new package, please check lerna create
!!
How to install a dependency?
This is most common question when develop use lerna in their project first time. You still can use yarn or npm to install dependencies in each package but it’s a little bit inefficiency. Lerna give you lerna add
command so that you can install a dependency to either all packages or specific packages.
For example:
$ lerna add react
This command will install react
to all the packages. If the dependency is just for development, you can add --dev
option.
In addition, if the dependency is just for some specific packages, you can use --scope
option:
$ lerna add react --scope=module1
However, there will be another question is about can I install react
in multiple packages? The answer seems negative. You have to execute lerna add one by one or use prefix:
$ lerna add react packages/react-bootstrap-*
How to run a command in each package?
Lerna provide lerna exec
for developer to run any command in all packages. For example:
$ lerna exec -- rm -rf ./node_modules
Be carefully, the double-dash is necessary. In addition, just like lerna add
or other lerna command, lerna exec
respect --scope
option so that you can specify what package you want to execute.
There’s one more thing that if you are trying to execute some npm command ex: npm test
, lerna give you another good way to execute: lerna run
$ lerna run test # Same as execute npm test in each package
How to publish packages?
Publish your packages to npm registery is most difficult and complicated things in monorepo management. lerna publish
command can help you to achieve that and it is worth to introduce lerna publish
in another article.
lerna publish
will try to publish the packages that have changed since the last release. Following is the things this command does:
- Add a new commit to change the version in
package.json
- Publish to npm
- Create a git tag and push to remote git repository.
Anyway, there are some interesting things can be introduced, I will share more in another article.
How to publish to private npm registry?
The most quick way is add --registry <url>
option when do the publish. Sometime this is useful when you don’t want to configure the registry configuration in all package.json file.
However, the best practice is configure the publishConfig
in your package.json:
"publishConfig": {
"registry": YOUR_REGISTRY_URL_HERE
}
In addition, make sure you configure your private registry url in .npmrc
file:
registry = YOUR_REGISTRY_URL_HERE
Conclusion
In the next article, I will introduce more about lerna publish
and give a long introduction about how to integrate react, webpack and lerna. Anyway, hope this article can help you and any feedback is welcome.