This is a technical summary post on how to distribute a simple NPM package, install it, use it, and then delete it.

Register an NPM account

Start by signing up for an account on the NPM website

Create NPM package

Create an empty folder and execute NPM init

Package name: (npm-public) npm-test version: (1.0.0) Description: This is a test entry point: (hello.js) test command: git repository: keywords: author: kim license: (ISC) MIT About to write to /Users/mac/dongzhiqin/npm-public/package.json: { "name": "npm-test", "version": "1.0.0", "description": "this is a test", "main": "hello.js", "scripts": {"test": "echo \"Error: no test specified\" && exit 1" }, "author": "dongzhiqin", "license": "MIT" } Is this OK? (yes) yesCopy the code

Fill in description, name, author, then log in NPM, run NPM adduser, and fill in the user name and password you just registered. Create a hello.js file and simply print a Hello World.

exports.sayHello = function () {
    return 'Hello, world.';
};

Copy the code

Release NPM package

After login is complete, you can publish, execute NPM public., and publish to NPM official website. I have a problem here.

npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/npm-test - You do not have permission to publish "npm-test". Are you logged in as the correct user?
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mac/.npm/_logs/2021-02-06T09_28_44_843Z-debug.log
Copy the code

This problem is usually caused by the duplicate name of the package. Later, I changed the name of the package dongzhiqin and uploaded it successfully.

$NPM publish. NPM notice NPM notice 📦 [email protected] NPM notice === Tarball Contents === NPM notice 64B hello.js npm notice 230B package.json npm notice === Tarball Details === npm notice name: dongzhiqin npm notice version: 1.0.0 NPM notice Package Size: 326 B NPM notice Unpacked size: 294 B NPM notice SHasum: a6919f2eed104070dc353c7325dfc5972c9ef168 npm notice integrity: sha512-mgx6G4Q++itS5[...] VC9uMezuLTJNA== NPM notice Total Files: 2 NPM notice + [email protected]Copy the code

Go to the official website to search, you can indeed search the package we uploaded, has been released successfully

Install and use the NPM package

Now you can test it out. Create a new folder and run NPM init -y to quickly create package.json.

Run NPM install dongzhiqin

Look at the package already in package.json

{" name ":" NPM - test ", "version" : "1.0.0", "description" : ""," main ":" index. Js ", "scripts" : {" test ", "echo \" Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "dongzhiqin": "^ 1.0.0}}"Copy the code

Let’s use it. Create an index.js with the contents of

const { sayHello } = require('dongzhiqin')
console.log(sayHello())
Copy the code

Run node index.js in terminal and see hello World returned


mac at yuansus in ~/dongzhiqin/npm-test
$ node index.js
Hello, world.
Copy the code

Delete the NPM package

NPM unpublish dongzhiqin –force

reference

NPM config get registry / / view the NPM mirror current source NPM config set registry HTTP: / / https://registry.npm.taobao.org/ / / setting NPM mirror image source for taobao yarn Config get registry / / check the yarn mirror current source yarn config set registry HTTP: / / https://registry.npm.taobao.org/ / / setting yarn for taobao mirror image sourcesCopy the code
npm --- https://registry.npmjs.org/

cnpm --- https://r.cnpmjs.org/

taobao --- https://registry.npm.taobao.org/

nj --- https://registry.nodejitsu.com/

rednpm --- https://registry.mirror.cqupt.edu.cn/

npmMirror --- https://skimdb.npmjs.com/registry/

deunpm --- http://registry.enpmjs.org/
Copy the code

The reference is to the little Match front-end station -nodejs

Find a good series of articles NPM release package tutorials