Node Learning (4) - npm overview - use npm to install third-party modules for global installation and local partial installation

Node Learning (4) - npm overview - use npm to install third-party modules for global installation and local partial installation

1. npm overview

1.1 Third-party modules

  • After Node is installed, you can use some modules, such as fs, path, http, etc. These modules are called built-in modules or core modules.
  • Third-party modules are also called "packages" or third-party libraries.
  • Third-party modules are not modules that come with the Node software, but are modules written by others. But we can download it and use it.
  • Most third-party modules are used to simplify development and form modules after encapsulating complex code.
  • To use third-party modules, you need to install them first (download from the Internet)

1.2 npm overview

  • npm (node ​​package manager) translates to node package (module) manager
  • npm is a management tool for installing and uninstalling third-party modules
  • There is no need to install npm separately. After installing node, npm will be installed by default.
  • Try to execute the npm -v command to see if you can see the npm version

1.3 Use npm to install third-party modules

1) Global installation

Generally installed globallyOrder, so globally installed modules can call this command in any folder.

The contents of global installation are generally in the system disk. You can view the global installation path through npm root -g.

Syntax format for global installation:

# 安装语法
npm install -g 模块名

# 新版Mac系统,如果提示权限不足,可以使用 sudo su npm install -g 模块名

Execute the following command to install the first third-party module globally

npm install -g nodemon
  • npm means using the npm tool
  • install means installation, which can be abbreviated as i
  • -g means global installation
  • nodemon is the name of the third-party module

nodemon is a tool for automatically starting services, a command similar to node

After global installation, you can use this command anywhere

Third-party modules installed globally are generally commands

Insert image description here

2) Local installation

The modules installed locally are modules that can be used directly. Theynot an order is a module similar to fs that can be loaded through require().

In the folder where you need to use the third-party module, execute the following command to install the third-party module locally.

npm install 模块名
  • npm means using the npm tool
  • install means installation

In the future, local installation will be used extensively.

Install express locally below

  • In the folder where you write code today, open the terminal
  • Excuting an ordernpm install express
  • After the installation is completed, the node_modules folder and package-lock.json file will be generated in the code
    • node_modules — The third-party module express files we just downloaded are all here
    • package-lock.json — It records the download address, version, etc. of the content in express.
  • In the code folder, express is installed, which is a local installation. After the installation is completed, express can only be used in the code folder or in the sub (descendant) directory of the code. Express cannot be used in other folders.

In subsequent studies, we basically use third-party module development because their encapsulation methods are simpler and easier to use.

Guess you like

Origin blog.csdn.net/weixin_44867717/article/details/128891438