Lerna tutorial

What is Lerna?

Multiple package management tools make it easy to manage multiple NPM packages in a project and unify the version number of each package when it is shipped.

Gracefully solve the problem of manually changing the version number of each package and releasing it separately in large multi-package projects.

Second, environment building

Open the lerNA installation environment on the terminal

npm i lerna -g

or

yarn global add lerna
Copy the code

After the installation is complete, run lerna -v to check whether the lerna version number can be correctly output.

Three, use,

Initialize the project

Find an empty folder and execute lerna init to initialize the project.

The directory structure after initialization is as follows

. ├ ─ ─ lerna. Json// LerNA configuration├ ─ ─ package. Json └ ─ ─ packages// Specify that this folder contains multiple 'NPM' packages.
    |
Copy the code

Creating a project package

Create projects from the command line

lerna create one
Copy the code

After entering the pkG. json content in the terminal, you can see that the One project has been created in the Packages folder.

Now go ahead and execute lerna create two to create the second package.

The directory structure is as follows:

. ├ ─ ─ lerna. Json ├ ─ ─ package. The json └ ─ ─ packages ├ ─ ─ one │ ├ ─ ─ __tests__ │ ├ ─ ─ lib │ ├ ─ ─ the README. Md │ └ ─ ─ package. The json └ ─ ─ Two ├ ─ ─ __tests__ ├ ─ ─ lib ├ ─ ─ the README. Md └ ─ ─ package. The jsonCopy the code

lerna bootstrap

Each package will have a separate dependency that needs to be installed. Now we will add a dependency under the One package.

packages/one/package.json

{..."dependencies": {
    "classnames": "^ 2.2.6." "},... }Copy the code

Execute at terminal

lerna bootstrap
Copy the code

These dependencies will be installed in the package for you.

Four, publish,

Login NPM

npm login
Copy the code

If you do not have an NPM account, you can register one on the NPM website.

release

Now we publish the two packages under Packages to NPM. Perform:

lerna publish

info cli using localVersion of LERna LERna notice CLI V4.0.0 LERna info current version 0.0.0 lerna ERR! ENOCOMMIT No commitsin this repository. Please commit something before using version.
Copy the code

Err: There are no commits in this repository

This is an error.

There are no commits in this repository. Please submit something before using the version.Copy the code

To distribute packages on NPM, you need a Git repository.

Create a Git repository and submit your code first.

To prevent submitting the node_modules folder, add the.gitignore file to the root directory.

/node_modules
/packages/*/lib
/packages/*/dist
/packages/*/node_modules
Copy the code

Add git address to package.json for each package:

/packages/one/package.json:

{..."repository": {
    "type": "git"."url": "https://github.com/hang1017/lernaStudy"},... }Copy the code

Err: There is uncommitted code in the project

Now lerna publish

lerna ERR! EUNCOMMIT Working tree has uncommitted changes, please commit or remove the following changes before continuing:
lerna ERR! EUNCOMMIT  M packages/one/package.json
lerna ERR! EUNCOMMIT  M packages/two/package.json
Copy the code

This error message indicates that our project has some content that has not been committed to Git.

Before sending a package, submit the code before sending it.

Lerna publish again

lerna publish ? Select a new version (currently 0.0.0) (Use arrow keys) ❯ Patch (0.0.1)------------------ Minor version (0.1.0 from) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Major version (1.0.0) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the big version Prepatch (0.0.1 - alpha. 0) Preminor (0.1.0 from - alpha. 0) Premajor (1.0.0-alpha.0) Custom Prerelease Custom VersionCopy the code

Then choose Patch to release a small version of the package.

Error message appears again:

lerna notice Skipping all user and access validation due to third-party registry lerna notice Make sure youre Authenticated properly ¯ _(ツ)_/¯ LERna WARN ENOLICENSE Packages one and two are missing a license. Lerna WARN ENOLICENSE  One way to fix this is to add a LICENSE.md file to the root of this repository. lerna WARN ENOLICENSE See https://choosealicense.comfor additional guidance.
lerna http fetch PUT 403 http://registry.npmjs.org/one 615ms
lerna ERR! E403 You do not have permission to publish "one". Are you logged in as the correct user?
Copy the code

Err: 403 No permission \ Add license

Because the NPM has more than one hundred bags, so we use one | | two simple name of the package will be as and other package name repetition lead to do not have permission to this package to be released. There are private packages that you can’t find on NPM, but they do exist. so

1, you need to modify the one | | two two package name.

2. Add “license” to package.json of both packages: “MIT”

3. Add the LICENSE file in the root directory as follows:

Modify the name and mailbox in the third line

The MIT License (MIT)

Copyright (c) 2017-present yournane (*********@qq.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Copy the code

Now let’s run lerna publish again to see if it can be published successfully.

There will still be 403 permission issues in my local.

I wonder if there are any kind people in society who can help look into this problem.

However, in order to distribute packages smoothly, we can set up a private organization to distribute packages under the name of a private organization.

Private packages

1, create,npmOrganization andgitOrganization.

Start by creating an NPM organization, using dlijs as an example.

And create a dliJS organization on Git. And migrate projects to this organization.

2. Modify the project warehouse name

Change the package.json of both packages to @dlijs/one and @dlijs/two.

Ok, so now we can try to send the package.

Under the author’s test, the package can be smoothly issued. The @dlijs/one and @dlijs/two packages are searchable on NPM.