preface

When you see someone else’s code you just need to use a line like this:

npm install xxx
Copy the code

You can download the entire code. Do you wish your code could be downloaded so easily? That’s the beauty of NPM.

About the NPM

NPM is the official package management tool for Node.js. It can be used to update, download, and upload Node packages (www.npmjs.com) with a few simple commands on the console. How to install Node.js and NPM will not be described in this article, you can find information online to follow the installation. If you haven’t played Node yet, install one and try it out with me. Make sure it’s a big sky.

Publish your own code in NPM

1. Verify the NPM

Create a new folder to save the package and go to that folder (Windows students can hold down shift + right click and select Open command window here to quickly bring up the console). First you need to verify that you have NPM installed. If you do, skip this step and type the command on the console:

npm -v
Copy the code

If some version numbers are displayed, you have installed:

2. The new package. Json

Create a new package.json file in the project root directory. This is a JSON file that describes your package, such as package name, other packages that depend on it, author, and so on. NPM initializers can be quickly created by typing commands in the root directory:

npm init
Copy the code

NPM will then prompt you to enter some information about your package. If you don’t know how to enter that item, just press Enter:You’ll find an extra package.json file in your root directory. Of course,nameNote that it will be the name of the NPM package you published, if you accidentally fill in the wrong name, you can directly create a new onepackage.jsonModify thenameField.

3. Write code

NPM allows you to publish an empty package (a package that contains only package.json), but such a package doesn’t make sense. If we don’t have code, we can add a readme. md to illustrate our package: Create a new readme. md file in the root directory and type the following:

# This is one of my NPM packages to be continued…

If you want to write instructions (JS, HTML, CSS, etc.) please try to put them in.

4. Create an NPM account

If you have not created an NPM account before, enter the following command to create an NPM account and follow the instructions to fill in the account and password:

npm adduser
Copy the code

5. Release!

It is now ready to publish. Again, type the command in the root directory:

npm publish
Copy the code

If you see something like this, congratulations on having published an NPM package:

So you can be in thewww.npmjs.comSearch and find the name of the NPM package you just published (for example, mine is: article-npm-publish, if you need to log in, please use the account you just registered on the console) :

Now you can try using NPM install XXX (XXX is your package name) to install your own package.

Q&A

1. If you need to update the package, modify the version field of the package.json package after modifying the code, and then NPM publish. Otherwise, it will not publish;

2. If a message like ‘please confirm whether you have permission to update XXX package’ is displayed in the release, it means that your package name is in use. Just change the name.

3. If you want to delete a package you have published, please use NPM unpublish –force XXX (XXX is the package name).