Introduce NPM package management

npm package management

1 Introduction

Npm is a NodeJs project module management tool. It has been integrated into the nodejs installation package (since 5.2, new npxinstructions have been added to solve the cumbersome problem of calling the modules installed inside the project). Using npm can download the first written by others from the NPM server. Three-party package to local use.

npm warehouse address: https://www.npmjs.com/

Of course, in addition to npm, there is also Yarn contributed by Facebook, which has the same function as npm and can also be used as a package management tool.

npx

node-modules/.bin/mocha --version
# 简化成了:
npx mocha --version

2. Switch npm source

Source: source station 镜像源,, mirror.

Npm uses the foreign mirror source address, and sometimes the network is not very smooth. At this time, you can use the domestic mirror source to complete the npm download module function.

Switch source

  • Switch to the npm mirror source provided by Ali

Address: https://developer.aliyun.com/mirror/NPM?from=tnpm

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

After performing the above naming, a cnpm package management tool is provided in the system. The function is the same as npm, but the difference is that the cnpm mirror source address is the source address provided by Ali.

Recommend, the future job is to install software using cnpm

  • Use nrm to manage npm mirror sources

nrm is an npm source manager that allows you to quickly switch between npm sources. By default, npm uses the official npm source (npm config list to view). If you modify the npm source directly, if you need to connect to the official source in the future to work, it will become troublesome to switch the source back and forth, and nrm can do it with a simple command Solve this problem.

# 安装  通过cnpm来安装,cnpm使用的就是国内镜像源
cnpm i nrm -g

Note: -g means globalglobal, so that nrm is not limited to a certain project, but can be used in all projects

After installing nrm, you can use the nrm lscommand to view the effect:

nrm ls

*Indicates the mirror source currently in use

If you want to switch to another mirror source, you can nrm use 源名称switch, if you need to switch to edunpmthe source, you can execute the command:

nrm use edunpm

3. npm related commands

# 初始化生成package.json文件(创建项目的)
npm init -y[不询问]

# 查看本项目已安装模块
npm list

# 安装模块
npm install 模块名[@版本号 可选]npm i 模块名[@版本号 可选]

# 卸载已安装模块
npm uninstall 模块名

# 查看全局node_modules的地址
npm root -g

## 安装参数
##--save -S     记录生产环境所需模块 默认
##--save-dev -D 记录开发环境所需模块
##-g 安装到全局

on语义化版本

Version format: major version number. minor version number. revision number. previous version number . The version number increment rules are as follows:

  • Major version number: when you make incompatible API changes

  • Minor version number: when you have added a downward compatible function (the function has been upgraded)

  • Revision number: When you have made a downward compatibility problem correction (daily bug modification)

  • The previous version number and version compilation metadata can be added to 主版本号.次版本号.修订号the back as an extension.

~Indicates that the version number can only change the last paragraph

^Indicates that in addition to the major version number, both the minor version number and the patch version number can be changed

0The version number at the beginning of the special case :

Dangerous writing: ~0.1, equal to 0.1.0 <= version number <1.0.0

Insurance writing: ^0.1, equal to 0.1.0 <= version number <0.2.0

About the four stages of software version release:

  • alpha: closed beta version
  • beta: Beta version
  • release candidate: RC, the candidate version, the function has been determined, mainly to eliminate bugs
  • release: the official release version

4. Custom npm script

Customize script commands through scripts in the package.json file:

{
    
    
  "scripts": {
    
    
    "test": "echo hello"
  }
}

Then you can run it on the command line:

npm run test

Custom npm script

If you need more custom commands, just apply them in the above format.

5. Automatically restart the application

When writing and debugging Node.js projects, you need to manually restart the application frequently after modifying the code, which is very cumbersome. Nodemon is a tool whose function is to monitor changes in code files and automatically restart when the code changes .

# 全局安装nodemon
npm i -g nodemon

# 执行node脚本
nodemon app.js

nodemon

References: NPM official documents

Guess you like

Origin blog.csdn.net/qq_43377853/article/details/110870926