Publish your own NPM package

1 register your ownnpmaccount

  • Go to the NPM official website to register an account

2 Login Account

npm login
Copy the code

The installation NRM

npm install nrm -g
nrm ls
nrm use npm
Copy the code

4 Create a project folder

mkdir js-tools && cd js-tools
Copy the code

5 NPM initialization

npm init -y
Copy the code

6 the configuration package. Json

{
  "name": "wxdxjs-tools".  "version": "1.0.0".  "description": "this is my first package".  "main": "index.js". "private": false. "scripts": {  "test": "echo \"Error: no test specified\" && exit 1"  },  "author": "chenwei". "license": "ISC" } Copy the code

7 create README. Md

this is my package
Copy the code

8 create index. Js

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

The directory structure

─ ─ js - tools├ ─ ─ the README, md├ ─ ─ index. Js└ ─ ─ package. JsonCopy the code

9 NPM release

npm publish
Copy the code

10 Version Iteration

You need to change the package.json version number and add it each time as required

+ "version" : "1.0.0"
- "version" : "" 1.0.1,
Copy the code

11 NPM cancels publication

npm unpublish wxdxjs-tools --force
Copy the code

It may be wrong

code E402
402 Payment Required - PUT 
http://registry.npmjs.org/@dinghx%2funifiedValidator 
- You must sign up for private packages
Copy the code

This is because the package name is @xxx/ XXXX. NPM will think that to publish private packages, private packages need to be charged, so it needs to change the issue command to

npm publish --access public
Copy the code
code E403
Code E403 has no permission to publishnpm ERR! [no_perms] Private mode enable, only admin can 
publish this module [no_perms] 
Private mode enable, only admin can publish
this module: your-package
Copy the code

This is only possible when your source is set as a third party source, such as taobao source may cause this problem. Just change the source back to the default, as follows

npm config set registry http://registry.npmjs.org
Copy the code
The package name is similar to
npm ERR! Package name too similar to existing packages; 
try renaming your package to '@dinghx/captcha' and publishing 
with 'npm publish --access=public' instead : your-package
Copy the code
The mailbox is not verified.
npm ERR! you must verify your email before publishing a new package
: https://www.npmjs.com/email-edit : your-package
Copy the code

This is the mailbox is not verified, log in https://www.npmjs.com to initiate mailbox verification again

Done