Node.js package manager NPM explained

Time:2022-8-5

The package manager, also known as the package management system, is a combination of tools for automatically installing, preparing, uninstalling and upgrading software packages in the computer. It is widely used in the installation and management of various system software and application software. It is also very beneficial to our business development, and the same thing does not have to be repeated to build wheels.

Each tool or development language has a corresponding package manager, such as Ubuntu's apt-get, Centos' yum, Java's Maven repository, and so on. The most famous package manager in Node.js is NPM and the best ecology.

What is NPM?

NPM is the package manager in Node.js. Allowing us to install various modules for Node.js, this package manager provides us with install, remove and other commands to manage modules. One thing we need to pay attention to here, 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 in 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"
  }
}
// module Y
{
  "name": "Y",
  "dependencies": {
    "A": "^1.5"
  }
}

When do I need an NPM package?

When we are developing some Node.js projects, we may encounter some places that require NPM, such as linking Redis, MongoDB or sending requests, etc. Having these existing modules can make us more focused on business development, of course, sometimes you will For some special requirements, you may need to encapsulate an NPM module yourself.Implement module reuseResource Sharing

npm install

NPM does not need to be installed separately. When we install the Node.js environment, NPM is also installed. Students who have not yet built the Node.js environment can refer to "3N Brothers" to help you complete the Node.js environment construction section.

Execute the npm -v command on the terminal to view the current npm version

$ npm -v

5.6.0

npm source settings

In China, it is sometimes limited by the influence of network factors. Usually, before installing a package manager, you can switch to the taobao source, which makes the speed faster. However, it should be noted that if the private module is official in NPM, it must be switched to the official source. , otherwise a 404 error will occur.

View current npm sources

  npm config get registry
  # http://registry.npmjs.org/

Switch to taobao source

npm config set registry=https://registry.npm.taobao.org

Switch to npm official source

You need to switch back to npm source when npm publish

npm config set registry=http://registry.npmjs.org

How to apply in the project

Let's create a new project test, which is an empty folder at first

first step

Execute npm init on the console, enter information according to the prompt, and generate a package.json file, as shown below:

{
  "name": "test", // project name
  "version": "1.0.0", // version number
  "description": "", // description
  "main": "index.js", // entry file, default index.js
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "May", // author
  "license": "ISC"
}

second step

Install the npm module, for example, we install a moment module to format the time, execute the following command

npm install moment -S
# or 
npm i moment --save

After the installation is successful, a new directory node_modules will be generated, which is used to store the modules we installed. In addition, package.json will also change and a dependencies object will be added, which is used to store our module version information.

"dependencies": {
  "moment": "^2.24.0"
}

Take a look at our current directory structure:

Node.js package manager NPM explained

NPM Register Login

register

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

View current users

npm whoami

npm login

npm login

private module

If it is a private npm package of a company team or a personal project, pay attention when publishing it. The name of the module should start with@symbol starts,/The symbol ends, and the middle part is the organization name of the private package. E.g,@may/logger, may be the name of the organization, and logger is the package name.

package.json

{
  "name": "@may/logger"
}

npm-module-publish

Enter the project root directory and enter the command.

npm publish

common problem

Questions1

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

It should be noted here that due to domestic network problems, many small partners have proxyed npm's mirroring to Taobao or other places. Here, the original mirroring must be set back.

npm config set registry=http://registry.npmjs.org

Questions2

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

Excuting an ordernpm cache clean --force

Questions3

Node project deployment private package error 404 is generally caused by two situations:

  • Check if the server is logged in to the npm account
  • Excuting an ordernpm config get registryCheck if it points to https, and execute the command if it does not point to httpsnpm config set registry=https://registry.npmjs.org