1. What is NPM?

NPM is the module manager for Node and is extremely powerful. It’s one of the big reasons for Node’s success. Thanks to NPM, we can install modules written by others with a single command.

The basic knowledge of NPM is not described here. The following provides some reference information about the installation mechanism of the NPM module on the OFFICIAL website

How to create your first Node module and upload it to NPM so that others can use it


2. Create the first Node module

Node.js modules are packages of code published to NPM,

The first step in creating a new module is to create a package.json file. You can create package.json files with NPM init. During this process, you will be prompted to enter the module information step by step. The module name and version number are mandatory

The created package.json looks like this


You will also need an entry file, which will be index.js if you use the default. Json file, you need to write the contents of the package. The simplest example is to export a function in the default index.js file that can be imported or required from someone else’s code.

exports.showMsg = function () {
  console.log("This is my first module");
};Copy the code

In this case, your Node module will have been created

3. How do I publish to the NPM server

3.1. There are two ways to register an NPM account. The easiest way is to register on the official website of NPM
  • NPM registry
  • Another option is to follow NPM adduser’s instructions
3.2. Login is required for the first time,npm loginStore the certificate to the local, then you do not need to log in every time

To log in, you need to enter your user name, password, and email address, which are filled in when you first register

3.3. Start publishing

NPM publish publishes the entire directory. Modules that do not want to publish can be ignored through.gitignore or.npmignore

After the successful release, you can go to the NPM official website and search to see if the NPM already exists

This is one of the problems that the release process may encounter

CNPM error: no_perms Private mode enable, Only admin can publish this module NPM config set registry registry.npmjs.org If you want to return to the previous CNPM, use the following command npm config set registry registry.npm.taobao.org

4. Create a case that references the package you just uploaded

If the package name is toniqian-test-module, create an empty directory, CD it, and run NPM install toniqian-test-module. Then a folder called node_modules will appear in this directory, where the packages you wrote will appear.

Then write an index.js as follows

var test = require('toniqian-test-module');
test.showMsg();Copy the code

Running index. Js

node index.jsCopy the code

Running result display

This is my first moduleCopy the code

So the package you just uploaded is now available, and so far you have successfully created an NPM package

5. How to update the NPM package

When the contents of your package are modified, for example

exports.showMsg = function () {
  console.log("This is my second module");
};Copy the code

Json will automatically update the version number in package.json and then re-npm publish, the update will be complete

6. Summary

This is how you create an NPM package. If you have any questions, please leave a comment. Thank you