Introduction to the use of NPM in Node.js

Introduction to NPM

NPM is a package management tool installed with NodeJS, which can solve many problems in NodeJS code deployment

  1. Allow users to download third-party packages written by others from the NPM server to local use.
  2. Allow users to download and install command line programs written by others from the NPM server for local use.
  3. Allow users to upload their own packages or command line programs to the NPM server for others to use.

Since the new version of nodejs has integrated npm, npm has also been installed before. You can also enter "npm -v" to test whether the installation is successful

$ npm -v
2.3.0

In the process of downloading and installing npm, the network connection is often unstable, so it is highly recommended to use the domestic Taobao mirror to download.

npm install -g cnpm --registry=https://registry.npm.taobao.org

Install the module using the npm command

The syntax format of npm install Node.js module is as follows:

$ npm install <Module Name>

In the following example, we use the npm command to install the commonly used Node.js web framework module express:

$ npm install express

After installation, the express package is placed in the node_modules directory under the project directory, so you only need to pass require('express') in the code without specifying the third-party package path.

var express = require('express');

Global installation and local installation

The package installation of npm is divided into two types: local installation and global installation. From the command line, the only difference is whether there is -g, such as

npm install express          # 本地安装
npm install express -g   # 全局安装

If the following error occurs:

npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 

The solution is:

$ npm config set proxy null

Local installation

  1. Put the installation package under ./node_modules (the directory where the npm command is running). If there is no node_modules directory, the node_modules directory will be generated under the directory where the npm command is currently executed.
  2. You can use require() to introduce locally installed packages.

Global installation

  1. Put the installation package under /usr/local or your node installation directory.
  2. It can be used directly in the command line.
    If you want to have both functions, you need to install it in two places or use npm link.

Next we use the global method to install express

$ npm install express -g The
following content is output during the installation process. The first line outputs the version number and installation location of the module.

[email protected] node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])

View installation information
You can use the following command to view all globally installed modules:

$ npm list -g
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
……

If you want to check the version number of a certain module, you can use the following command:

$ npm list grunt
projectName@projectVersion /path/to/project/folder
└── [email protected]

Uninstall the module

We can use the following command to uninstall the Node.js module.

$ npm uninstall express

After uninstalling, you can go to the /node_modules/ directory to check whether the package still exists, or use the following command to check:

$ npm ls

Update module

We can update the module using the following command:

$ npm update express

Search module

Use the following to search for modules:

$ npm search express

Create module

To create a module, the package.json file is essential. We can use NPM to generate a package.json file, which contains the basic results.

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (node_modules) runoob                   # 模块名
version: (1.0.0) 
description: Node.js 测试模块(www.runoob.com)  # 描述
entry point: (index.js) 
test command: make test
git repository: https://github.com/runoob/runoob.git  # Github 地址
keywords: 
author: 
license: (ISC) 
About to write to ……/node_modules/package.json:      # 生成地址

{
    
    
  "name": "runoob",
  "version": "1.0.0",
  "description": "Node.js 测试模块(www.runoob.com)",
  ……
}


Is this ok? (yes) yes

You need to enter the above information according to your own situation. After entering "yes" at the end, the package.json file will be generated.

Next, we can use the following command to register users in the npm resource library (register with email):

$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]

Next, we will use the following command to publish the module:

$ npm publish

If you have performed the above steps correctly, you can use npm to install it just like other modules.

Common NPM commands

In addition to the parts introduced in this chapter, NPM also provides many functions, and there are many other useful fields in package.json.

In addition to viewing official documents at npmjs.org/doc/, here are some common NPM commands.

  1. Use npm help to view detailed help for a command, such as npm help install.

  2. Use npm install in the directory where package.json is located. -g can first install the current command line program locally, which can be used for local testing before release.

  3. Use npm update to update the corresponding modules in the node_modules subdirectory of the current directory to the latest version.

  4. Use npm update -g to update the corresponding command line program installed globally to the latest version.

  5. Use npm cache clear to clear the NPM local cache, which is used to deal with people who use the same version number to release a new version of the code.

  6. Use npm unpublish @ to unpublish a certain version of the code that you have published.

Guess you like

Origin blog.csdn.net/david2000999/article/details/114971054