In our last article, “NPM Release Package Tutorial (1) : From NPM”, we introduced some knowledge about NPM to give you some in-depth understanding of NPM. In this article, we begin to demonstrate the release process.

First, preparation

Before we can start the demo, we need to do two things:

1. Register an NPM account

The registered address

  • Full name:
  • Email address:
  • User name:Important! You'll need it when you release the Scoped package
  • Password:

2. Install the NRM globally

npm i nrm -g
Copy the code

NRM is the software of NPM warehouse management, which can be used for fast switching of NPM warehouse

NRM common commands:

NRM // Displays the available NRM command NRM ls // lists all configured warehouse NRMtestNRM add <registry> <url> // Add new repository NRM use <registry> // Switch repositoryCopy the code

Release package

Two brief notes to begin the demo: (1) The NPM official recommended specification package contains at least:

  • Package. json (Basic package information)
  • Readme.md (documentation)
  • Subsequent demos of index.js (the entry file) follow this specification.

(2) This demo package is for personal accounts only, including one unscoped package and one scoped package. The package distribution process under a group account is not very different from that of an individual account and will not be expanded here.

1. Release unscoped packages

yuyy-test-pkg

Step 1: Create the project

(1) Create a project folder

mkdir yuyy-test-pkg && cd yuyy-test-pkg
Copy the code

Create package.json

npm init
Copy the code

Follow the instructions step by step, or use NPM init -y to use the NPM default Settings and modify them later by editing package.json. Note: The package entry file for this demo is index.js. Make sure that the value of the main field in package.json is “index.js”. Final result:

{
  "name": "yuyy-test-pkg"."version": "1.0.0"."description": "my first npm package"."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm"."packge"]."author": "yuyy"."license": "ISC"
}

Copy the code

(3) Create readme. md content:

### yuyy-test-pkg

This is my first npm package!

It is just for learning.
Copy the code

Create index.js content:

module.exports = {
    printMsg: function () {
        console.log('this message is from yuyy-test-pkg! '); }}Copy the code

The final directory structure:

├─ ├─ download.txt ├─ ├─ download.txtCopy the code

Step 2: Publish

npm publish
Copy the code

Possible error:

(1) No login

npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
npm ERR! need auth You need to authorize this machine using npm adduser
Copy the code

Solution: NPM adduser

  • User name (if you forget, go to the NPM website: Profile > Profile Settings)
  • password
  • email

(2) The warehouse address is incorrect

npm ERR! code E409
npm ERR! Registry returned 409 for PUT on http://r.cnpmjs.org/-/user/org.couchdb.user:yuyy: conflict
Copy the code

Cause: Through the command NRM ls check my warehouse address is CNPM, not NPM

Solution: Use NRM to switch to the NPM repository and run the NRM use NPM command

After the preceding problems are rectified, run the NPM publish command again. The publish succeeds

Step 3: Search the NPM website

There may be delays. You can’t see them right away.

2. Release scoped packages

@yuyy/babel

Step 1: Create the project

(1) Create a project folder

mkdir babel && cd babel
Copy the code

Create package.json

npm init
Copy the code

Operation as prompted, final result:

{
  "name": "babel"."version": "1.0.0"."description": "my scoped test package"."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm"."package"]."author": "yuyy"."license": "ISC"
}
Copy the code

(3) Create readme. md content:

### @yuyy/babel

This is my scoped npm package!

It is just for learn.
Copy the code

Create index.js content:

module.exports = {
    printMsg: function () {
        console.log('this message is from @yuyy/babel! '); }}Copy the code

The final directory structure:

├─ ├─ ├─ download.txtCopy the code

Step 2: Publish

npm publish
Copy the code

Error: No publish permission

npm ERR! publish Failed PUT 401
npm ERR! code E401
npm ERR! This package requires that publishers enable TFA and provide an OTP to publish. For more info, visit: https://go.npm.me/2fa-guide : babel
Copy the code

Reason: The Babel package already exists, and I’m not the publisher of Babel

Solution: The package name is the same as the domain name, first come, first served, if I have to publish a package called Babel, I can only scope it into my namespace

Step 3: Add scope

npm init --scope=@yuyy -y
Copy the code

The @ sign is followed by the username when you registered the NPM account. If you do not remember, you can use NPM whoami to query. The package.json command regenerates package.json, but adds scope to the package.

{
  "name": "@yuyy/babel"."version": "1.0.0"."description": "my scoped test package"."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm"."package"]."author": "yuyy"."license": "ISC"
}
Copy the code

The only change is that the name field has been changed from Babel to @yuyy/ Babel.

Step 4: Republish

npm publish
Copy the code

Error:

npm ERR! publish Failed PUT 402
npm ERR! code E402
npm ERR! You must sign up for private packages : @yuyy/babel
Copy the code

The reason:

  • npm publishCommand, the default is private publishing, seePublish command
  • As mentioned at the end of the last article, scoped’s package is distributed privately for a fee

Solution: If you don’t want to spend money, you can only release packages to the public, which is in line with NPM’s spirit of encouraging open source, similar to GitHub’s creation of repositories.

Step 5: Public release

npm publish --access public
Copy the code

Execution Result:

One thing to note: our project name is Babel and the final package name is @yuyy/ Babel. As you can see, the package name can be different from the project name, depending on the name field in package.json.

Step 6: NPM official website search

NPM Release Package tutorial (3) : Installation and Loading principles

Related articles:

1. NPM Release Package Tutorial (PART 1) : Starting with NPM

2. NPM Release Package Tutorial (II) : Release Package

3. “NPM Release Package Tutorial (iii) : Installation and Loading Principles”

4. NPM Release Package Tutorial (4) : Iteration

5. NPM Release Package Tutorial (5) : Discard/Delete