Detailed NPM package management tool, tutorial

NPM package management tools

1.1 Definitions: What is NPM

NPM full name Node Package Manager, which is a JavaScript package management tools, and is the default package management tool Node.js platform. It can be installed by NPM, share, distribute the code, manage project dependencies.

  • NPM can be downloaded from the server written in someone else's third-party packages to local use.
  • Download and install a command-line program written by someone else to use local NPM from the server.
  • You have written a package or command line program NPM can be uploaded to the server for others to use.

In fact, we can be understood as the NPM Maven front end. We can be easily installed and downloaded through npm, front-end engineering management.

The latest version of Node.js has been integrated npm tool, you must first install the machine in Node.js

Node.js official website Download:

After the installation is complete, view the current version nodejs and npm

  • View node.js version: node -v
  • View npm version: npm -v

 

 

1.2 NPM command operation

1.2.1 NPM initialization

npm init command to initialize the project:

Create a new folder, go to the folder, execute the following command to initialize the project through the command prompt window

 

 

 

Finally it generates package.json file, this is the package's configuration file, the equivalent pom.xml maven. It can be modified according to the needs.

 

1.2.2 Installation Module

npm install command is used to install a module, installation is divided into: local installation (local), the global installation (global) two kinds.

Local installation

JS libraries will be installed in the current directory where the command is executed, installation

 

Example: mounting express module

 

If the yellow is a warning message appears, can be ignored, please rest assured that you have successfully installed.

There will be a node_modules and package-lock.json folder in the directory

node_modules folder used to store downloaded js library (the equivalent of the local maven repository)

package-lock.json 是在 npm install 时候生成一份文件。

用以记录当前状态下实际安装的各个包的具体来源和版本号。

重新打开 package.json 文件,发现刚才下载的 jquery.js 已经添加到依赖列表中了.

 

关于模块版本号表示方式:

指定版本号:比如 3.5.2,只安装指定版本。遵循 “大版本.次要版本 小版本”的格式规定。

~波浪号 + 指定版本号:比如 ~3.5.2,安装 3.5.x 的最新版本(不低于 3.5.2),但是不安装 3.6.x,也

就是说安装时不改变大版本号和次要版本号。

^ 插入号 + 指定版本号:比如 ^3.5.2,安装 3.x 新版本(不低于 3.5.2),但是不安装 4.x.x,也

就是说安装时不改变大版本号。需要注意的是,如果 本号为0,则插入号的行为与波浪号相同,这是

因为此时处于开发阶段,即使是次要版本号变动,也可能带来 程序的不兼容。

latest:安装最新版本。

 

 

全局安装

将 JS 库安装到你的 全局目录 下

 

使用全局安装会将库安装到你的全局目录下。

查看全局安装目录

如果你不知道你的全局目录在哪里,执行命令:

 

如果安装时出现如下错误:

npm err! Error: connect ECONNREFUS 27.0.0.1:8087

解决方法,执行如下命令:

npm config set proxy null

 

1.2.3 生产环境模块

格式:

--save或 -S 参数意思是把模块的版本信息保存 package.json 文件的 dependencies 字段中(生产环境依赖)

 

在 package.json 文件的 dependencies 字段中

 

1.2.4 生产环境模块

格式:

--save-dev 或 -D 参数是把模块版本信息保存到 package.json 文件的 devDep ncies 字段中(开发环境

依赖),所以开发阶段一般使用它:

 

举例:

安装 eslint 模块,它是语法格式校验,只在开发环境依赖中即可

 

In devDependencies segment package.json file

 

1.3 Batch download module

After we downloaded some items from the Internet, I found that only package.json, no node_modules folder, then we need to pass command to download

These js libraries.

The command prompt, enter the directory package.json, execute the command:

 

At this point, npm will automatically download in package.json dependent js library.

 

1.4 View command module

1.4.1 View local mode module is installed

Option 1: You can install the package under the directory node_modules see whether there

Option 2: You can use the following command to see:

 

View the latest version of the module 1.4.2 Remote

 

For example:

 

View all remote module version 1.4.3

npm view <Module Name> versions

 

For example: to see all versions of the module jquery

 

npm view jquery versions

1.5 uninstall module

Unload a partial module

npm uninstall <Module Name>

 

Uninstall global module

npm uninstall -g <Module Name>

 

1.6 Configuration Taobao accelerated mirroring

1. Check the mirror addresses currently in use

 

2. Configure mirror Taobao address

 

1.7 Installation cnpm

1. Install cnpm

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

2. Use cnpm

cnpm install xxx

Guess you like

Origin www.cnblogs.com/whaleup/p/11517916.html