Package manager, also known as software package management system, is a combination of tools that automatically installs, configures, uninstalls and upgrades software packages in computers. It is widely used in the installation and management of various system software and application software. It is also beneficial for our business development, the same thing does not have to be repeated to build wheels.

Each tool or development language has its own package manager, such as Ubuntu apt-get, Centos yum, Java Maven repository, and so on. By far the best known package manager in Node.js is NPM, which is also the most ecological.

What is NPM?

NPM is the package manager in Node.js. Allows us to install various modules on Node.js, and the package manager provides us with other commands to manage modules such as install, delete, and so on. One thing to note here is that we must have a package.json file or node_modules directory to install modules locally.

The best thing about NPM is that it stores our installed dependencies locally, in the Dependencies object of package.json. For example, if A module X uses module A version 1.0 and module Y uses module A version 1.5, then either module X or Y will have its own local copy of module A.

/ / module X {" name ":" X ", "dependencies" : {" A ":" ^ 1.0 "}}Copy the code
/ / module Y {" name ":" Y ", "dependencies" : {" A ":" ^ 1.5 "}}Copy the code

When do YOU need an NPM package?

When we are developing node.js projects, we may encounter areas where NPM is needed, such as linking to Redis, MongoDB, or sending requests to Request. Having these existing modules allows us to focus on business development. At this time, you may need to encapsulate an NPM module to achieve module reuse and resource sharing.

NPM install

NPM does not need to be installed separately. When we install the Node.js environment, NPM is also installed. If you have not built the Node.js environment, please refer to “3N brothers” to help you complete the node.js environment construction section.

The terminal runs the NPM -v command to view the current NPM version

5.6.0 $NPM - vCopy the code

NPM source set

In China, sometimes affected by network factors, taobao source can be switched to before installing a package manager to make the speed faster. However, it should be noted that if the private module is official in NPM, it must be switched to official source; otherwise, a 404 error will occur.

View the current NPM source

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

Switch to Taobao source

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

Switch to the NPM official source

NPM publish requires switching back to the NPM source

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

How can it be applied in a project

Let’s create a new project, Test, which starts with an empty folder

The first step

The console executes NPM init and follows the prompts to generate a package.json file, as shown below:

{" name ":" test ", / / project name "version" : "1.0.0", / / version number "description" : ""," main "/ / description: "Scripts ": {"test": "echo \"Error: no test specified\" && exit 1"}, "author": "May", // author license: "ISC"}Copy the code

The second step

To install the NPM module, for example, if we install a moment module to format the time, execute the following command

npm install moment -S
# or 
npm i moment --save
Copy the code

Json is also changed to store the dependencies object, which stores the module version of our module.

"Dependencies ": {"moment": "^2.24.0"}Copy the code

Take a look at our current directory structure:

NPM register login

registered

$ npm adduser
Username: your name
Password: your password
Email: (this IS public) your email
Copy the code

View the current user

npm whoami
Copy the code

NPM login

npm login
Copy the code

Private module

If it is a private NPM package for a corporate team or individual project, be careful when publishing it. The module name should start with the @ sign and end with the/sign, with the organization name of the private package in the middle. For example, @may/logger, where may is the organization name and logger is the package name.

package.json

{
  "name": "@may/logger"
}
Copy the code

NPM Module – release

Go to the project root directory and enter the command.

npm publish
Copy the code

Q&A

Questions1

no_perms Private mode enable, only admin can publish this module: coorddistance
Copy the code

Note here because of the domestic network problems, many partners put NPM mirror agent to Taobao or other places, here to set back to the original mirror.

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

Questions2

Unexpected end of input at 1:3637 npm ERR! Egistry.npmjs.org/mkdirp/-/mkdirp-0.3.2.tgz "}, "engines" : {" node ":" * * "}Copy the code

Run NPM cache clean –force

Questions3

Error 404 Is reported when deploying the private package in the Node project.

  • Check whether the server is logged in to the NPM account
  • Execute the commandnpm config get registryCheck whether the command is pointing to HTTPS. If no, run the commandnpm config set registry=registry.npmjs.org