When we write a common JavaScript module and want to share it with others, we will release it to the official repository as NPM package. When we need to use it, we can install it through NPM install XXX.

So how do you distribute an NPM package?

Create a project

Suppose we want to create an NPM package that provides arithmetic methods, create a directory locally called Calculator and enter. Note: This name is only for illustration purposes, you need to use a unique package name, you can check your package name on the NPM website to see if there is a duplicate.

$ mkdir calculator
$ cd calculator/
Copy the code

Json file is created in the package.json directory.

$ npm init --yes # Use --yes to automatically answer questions in the creation process with y
Copy the code
// package.json

{
  "name": "calculator"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": ""."license": "ISC"
}
Copy the code

This file name and location need to be consistent with the main field in package.js. Add that we want to change the location and file as the project entry. You can manually modify the main field, such as SRC /index.js.

In the entry file, add and export an Add method

// index.js

module.exports.add = function add(a, b) { return a + b }
Copy the code

At this point, our module is complete. Publish now.

First, check to see if the local NPM Registry points to the official NPM repository, as most people have probably changed it to point to taobao images instead.

$ npm config get registry
http://registry.npmjs.org/
Copy the code

Login NPM

After confirming, log in to NPM

$ npm addUser If you don't have an account, create one
$ npm login   If you already have an account, log in directly
Copy the code

After entering the above command, will let answer several questions, such as Username, Password, Email, etc., are filled in correctly will automatically login success.

  • Note that the use ofnpm addUserAfter the registration is successful, an authentication email will be sent to the filled email address. You need to click the authentication link in the email address to jump to the authentication page, and then you can continue to use the NPM function.

release

Now it’s time for the exciting publishing, typing on the command line will yield output similar to the following

$ npm publishNPM notice 📦 [email protected] NPM notice === Tarball Contents === NPM notice 57B index.js NPM notice 222B package.json npm notice === Tarball Details === npm notice name: calculator npm notice version: 1.0.0 NPM notice Package Size: 328 B NPM notice unpacked size: 279 B NPM notice SHasum: c12fd4fd33d92627f754cc4b0d029b6b3df9c4b8 npm notice integrity: sha512-rzIopSWVPTszG[...] 8RAsoK6tzRYcg== NPM notice Total Files: 2 NPM notice + [email protected]Copy the code
  • Note that if this error occurs, it is possible that your package name or version conflicts with the package name or version already in the NPM repository, so it is recommended that you create a unique name and go to the NPM repositorywww.npmjs.com/If no package name is found, the package is available. topackage.json,nameField to the new available package name, and then againnpm publishCan.

Now go to the NPM website to search for the package name, it should be found.

To use it, use NPM install XXX just like any other NPM.

update

Suppose our NPM package makes a change, such as adding a multiplication function, as follows

// index.js

module.exports.add = function add(a, b) { return a + b }
module.exports.multiply = function multiply(a, b) { return a * b }
Copy the code

Since it is a new feature, our version number needs to be further advanced by 1️. You can manually change the version field to 1.1.0 in package.json.

You can also use the functionality provided by NPM to make version changes, such as

$ npm version minor  # change the major version number, which is used when upgrading a large version with break change
$ npm version major  # Modify the middle number, backward compatibility, new function when used
$ npm version patch  # Change the patch number to fix the bug
Copy the code
  • Here is a brief description of version number semantics, our three-digit version number is as follows1.0.0, which corresponds to the one aboveminor.major.patchTo read and modify the version number, follow this specification.

Hopefully, understanding the NPM package release process will make the Node.js module a little less mysterious and our understanding of it a little better.