What is NPM? NPM is the original third-party package management tool integrated on NodeJS

0. Register an NPM account

www.npmjs.com/

1. View the NPM image. If the NPM image has been changed before, reset it

npm config get registry
Copy the code

1.1 Resetting a Mirror Path

(Students in the wall are used to using Taobao mirror, which will not be able to access the NPM account)

npm config set registry https://registry.npmjs.org
Copy the code

After 1.2, it is necessary to cut back

npm config set registry https://registry.npm.taobao.org
Copy the code

2. Create your NPM package

Execute in your project’s Git repository:

npm init
Copy the code

Enter the NPM package information as prompted. Package. json is created.

Name: the package name of the project, first search NPM official website, or NPM install you want to use the package name, in case the name already exists. Version: indicates the version number, which will be used in future updates. Entry point: the project main entry, the default root directory of index.js, calls the first file accessed by this package.

Other options are not important, and you can modify the package.json file directly later.

3. Log in to NPM on the terminal

npm adduser
Copy the code

Enter the user name, password, and email address of the NPM account to log in successfully:

Logged in as xxx on https://registry.npmjs.org/.
Copy the code

4. Publish the NPM package

npm publish
Copy the code

You must verify your email before publishing a new package: www.npmjs.com/email-edit, open the website as prompted, enter the email configured in step 2, and then log in to the email and click the email for verification.

5. Obtain the NPM package

Create an empty directory and try:

npm install <package-name>
Copy the code

Create a test.js file in the root directory after downloading the package:

Var t = require(' XXX ') // XXX = console.log(t)Copy the code

Run Node test and console output is normal.

6. Update the NPM package

To change the version number in package.json, do the same:

npm publish
Copy the code

7. Cancel the NPM package

Theoretically, NPM packages are unique and cannot be deleted. Published packages cannot be revoked and can only be updated. However, you can use the following command to indicate that the package is no longer maintained:

NPM Deprecate XXX (package name) 'This package I no longer maintain'Copy the code

8. Simple scripts

Create the publish file at the root of the project you want to publish

#! The/usr/bin/env bash the set - e # modified NPM source address NPM config get registry NPM config set registry=http://registry.npmjs.org # Echo 'NPM' NPM login echo "..." NPM publish # changed back NPM source address NPM config set registry=https://registry.npm.taobao.org echo - e "\ n \ n release success" exitCopy the code

Run the sh publish command to enter the account information to publish each publication.

Change the version of the package first. The version must be different each time.