How to Use Lerna for Multi-package Management

Why Do We Use Lerna

Splitting a large code repository into multiple independently versioned packages is useful for code sharing. However, some changes can become cumbersome and difficult to track if they span multiple repositories. Also, tests that span multiple code repositories can quickly become very complex.

To solve these (and many other) problems, some projects split the code repository into multiple packages and put each package into a separate code repository. However, projects such as babel, react, angular, Ember, Meteor, Jest, and many others contain and develop multiple packages in a single code repository.

Lerna is a tool optimized for the workflow of managing multi-package code repositories using git and npm.

How to Use Lerna for Multi-package Management

1. npm Initialization

Create a new folder spring-breeze, enter npm init -y in the integrated terminal, and package.json will be generated in the root directory. Please refer to the following code.

{
  "name": "spring-breeze",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

2. Install Lerna

Install lerna globally.

Execute: npm i lerna -g.

After the installation is complete, execute: lerna init.

After the execution is completed, the directory structure appears like this.

- packages (directory)
- lerna.json (configuration file)
- package.json (project description file)

3. Initialize Two Packages

By default, packages are placed in the packages directory. To create a package, you can use the command lerna create [loc].

lerna create core packages/core
lerna create core packages/tools

Change packages/core/lib/core.js to:

'use strict';
console.log("I am the core package~~")

Change packages/tools/lib/tools.js to:

'use strict';
console.log("I am the tools package~~")

At this point the directory structure is:

--packages (directory)
   --core
     ...
     --lib
       --core.js
     --package.json
     -- README.md
   -tools
     ...
     --lib
       --tools.js
     --package.json
     -- README.md
- lerna.json (configuration file)
- package.json (project description file)
...

4. Modify the Package Name

In order to prevent duplicate names, the package name is generally changed to @organization/package. For example, the core of this project should be changed to: @spring-breeze/core; tools should be changed to: @spring-breeze/tools.

For example, core module: (packages/core/package.json)

{
  "name": "@spring-breeze/core",
  "version": "0.0.1",
  "description": "> TODO: description",
  "author": "******",
  "homepage": "",
  "license": "ISC",
  "main": "lib/core.js",
  "directories": {
    "lib": "lib",
    "test": "__tests__"
  },
  "files": [
    "lib"
  ],
  "repository": {
    "type": "git",
    "url": "******"
  },
  "scripts": {
    "test": "echo \"Error: run tests from root\" && exit 1"
  }
}

5. Install the Third-party Package Lerna Add

Add public dependencies: Execute in the root directory (the same in which directory): lerna add lodash. At this point, you will find that both core and tools are installed with lodash.

Add a separate dependency: lerna add jquery --scope=core. The core will be installed with jquery.

6. Remove Dependencies Lerna Clean

Executing lerna clean will remove node_modules from all packages.

7. Install All Dependencies Lerna Bootstrap

Executing lerna bootstrap will reinstall all dependencies.

8. Test the Local Package

If the two packages developed locally are related, debugging with lerna is very simple. You only need to write the version number of the local package you want to use into the dependency execution: lerna link. For example, use the tools module in the core module.

Package add dependencies: "@spring-breeze/tools": "^0.0.1". Reference is below.

{
  "name": "@spring-breeze/core",
  "version": "0.0.1",
  "description": "> TODO: description",
  "author": "******",
  "homepage": "",
  "license": "ISC",
  "main": "lib/core.js",
  "directories": {
    "lib": "lib",
    "test": "__tests__"
  },
  "files": [
    "lib"
  ],
  "repository": {
    "type": "git",
    "url": "******"
  },
  "dependencies": {
    "@spring-breeze/tools": "^0.0.1"
  },
  "scripts": {
    "test": "echo \"Error: run tests from root\" && exit 1"
  }
}

Execute lerna link after adding. Use packages/core/lib/core.js in the core. The reference is below.

'use strict';
require("@spring-breeze/tools")

When the node executes core.js, it will find the console print: I am the tools package~~. This enables local debugging.

9. Preparation Before Launch

After the debugging is completed, it can be released. The release process is as follows:

1. Register an npm account.

2. Create a new organization spring-breeze (depending on your own project).

3. Execute npm login.

4. Configured in package.json under core and utils respectively.

 "publishConfig": {
    "access": "public"
  }

5. Create a new gitignore.

*node_modules

6. Create a new git repository: add the project git remote add "your git repository" and submit the project to the git repository.

git remote add https://gitee.com/geeksdidi/spring-breeze.git
git add
git commit -m "initialize"
git push -u origin master

7. Create a new LICENSE.md (can be an empty file).

8. Modify a file and execute lerna publish.

Please note that every time a new version is released, the code is committed to git.

10. View Published Package

Log in to the npm official website to see the package you published.

Hope the above information is helpful to you.



Leave a reply



Submit