NodeJS modularization ② (npm and packages)

Bag

Third-party modules in Node.js are also called .

Just as computer and computer refer to the same thing,3rd party modules and packages refer to the same concept, but named differently.

Unlike built-in modules and custom modules in Node.js, packages are developed by third-party individuals or teams and are free for everyone to use.

Why do we need bags?
Since the built-in modules of Node.js only provide some low-level APIs, the efficiency of project development based on built-in modules is very low.
Packages are encapsulated based on built-in modules, provides a more advanced and more convenient API, which greatly improves the development efficiency.
The relationship between packages and built-in modules is similar to the relationship between jQuery and the browser's built-in API. ,

package download

There is an IT company abroad called npm, Inc. This company has a very famous website: https://www.npmjs.com/, which is the world's largest package sharing platform, you can search from this website to find Any package you need, as long as you have enough patience!

npm, Inc. provides a server with an address of https://registry.npmjs.org/ to share all the packages externally. We can download the packages we need from this server.

Search for the packages you need from the https://www.npmjs.com/ website and
download the packages you need from the https://registry.npmjs.org/ server

npm, Inc. provides a package management tool, we can use this package management tool to download the required packages from the https://registry.npmjs.org/ server to local use.
The name of this package management tool is Node Package Manager (npm package management tool for short), and this package management tool is installed on the user's computer along with the Node.js installation package.

You can execute the npm -v command in the terminal to check the version number of the npm package management tool installed on your computer

npm use

Here we take formatting time as an example:
if we don't use third-party packages, it is still a bit troublesome for us to format time:
we have to write a formatting module ourselves

// 1. 定义格式化时间的方法
function dateFormat(dtStr) {
    
    
  const dt = new Date(dtStr)

  const y = dt.getFullYear()
  const m = padZero(dt.getMonth() + 1)
  const d = padZero(dt.getDate())

  const hh = padZero(dt.getHours())
  const mm = padZero(dt.getMinutes())
  const ss = padZero(dt.getSeconds())

  return `${
      
      y}-${
      
      m}-${
      
      d} ${
      
      hh}:${
      
      mm}:${
      
      ss}`
}

// 定义补零的函数
function padZero(n) {
    
    
  return n > 9 ? n : '0' + n
}

module.exports = {
    
    
  dateFormat
}

If you use a third-party package:
① Use the npm package management tool to install the time-formatted package moment in the project
② Use require() to import the time-formatted package
③ Refer to the official API documentation of moment to format the time

Command to install package in project

If you want to install the package with the specified name in the project, you need to run the following command:

npm install 包的完整名称

The above packaging command can be abbreviated as follows:

npm i 包的完整名称

Next, we only need a few simple lines of code to complete the time formatting:

// 1. 导入需要的包
// 注意:导入的名称,就是装包时候的名称
const moment = require('moment')

const dt = moment().format('YYYY-MM-DD HH:mm:ss')
console.log(dt)

The API documentation of the package can be searched at https://www.npmjs.com/

What files are added after the initial packaging

After the initial packaging is completed, there is an additional folder called node_modules and a configuration file of package-lock.json under the project folder.

Among them:
The node_modules folder is used to store all the packages installed into the project. When require() imports a third-party package, it finds and loads the package from this directory.
The package-lock.json configuration file is used to record the download information of each package in the node_modules directory, such as the package name, version number, download address, etc.

The following figure is what the package-lock.json configuration file looks like after installing the moment package:
insert image description here

Note: Programmers should not manually modify any code in node_modules or package-lock.json files, they are automatically maintained by the npm package management tool.

Install the specified version of the package

By default, when you install a package with the npm install command, the latest version of the package is automatically installed. If you need to install a specific version of the package, you can specify the specific version with the @ symbol after the package name, for example:
insert image description here

Semantic Versioning Specification for Packages

The version number of the package is defined in the form of "dotted decimal", with a total of three digits, such as 2.24.0,
where each digit represents the meaning as follows:
1st digit: major version
2nd digit: function Version
3rd digit: Bugfix version

The rule of version number promotion: as long as the previous version number increases, the latter version number will be zeroed.

package management configuration file

npm stipulates that in the project root directory, a package management configuration file called package.json must be provided. Used to record some configuration information related to the project. E.g:

  • Project name, version number, description, etc.
  • What packages are used in the project
  • which packages are only used during development
  • Those packages are needed for both development and deployment

The problem of multi-person collaboration

insert image description here

The volume of the entire project is 30.4M The volume of the
third-party package is 28.8M The volume of the
project source code is 1.6M

From this, we can see that the volume of the third-party package is too large, which is inconvenient for team members to share the project source code.

Therefore, we stipulate that node_modules should be eliminated when sharing, and these third-party packages are allowed to be downloaded by members.

So how do we let other members of the project know which third-party packages are installed in this project?

How to record which packages are installed in a project

In the project root directory, create a configuration file called package.json, which can be used to record which packages are installed in the project. This makes it easy to share the source code of the project among team members after removing the node_modules directory.

Note: In the project development in the future, be sure to add the node_modules folder to the .gitignore ignore file.

Quickly create package.json

The npm package management tool provides a shortcut command to quickly create the package.json package management configuration file in the directory where the command is executed:
insert image description here

Notice:

  • The above command can only be successfully run in the English directory! Therefore, the name of the project folder must be named in English, not in Chinese, and no spaces can appear.
  • When running the npm install command to install a package, the npm package management tool will automatically record the package name and version number in package.json .
  • We should create this file at the beginning of a project, and we only need to enter this command once from start to finish

dependencies node

In the package.json file, there is a dependencies node dedicated to recording
which packages you have installed using the npm install command.

There is no dependencies node when you don't have a package installed

insert image description here

Install all packages at once

When we get a project with node_modules removed, we need to download all the packages to the project before we can run the project. Otherwise, an error similar to the following will be reported:
insert image description here

You can run npm installthe command (or npm i) install all dependencies at once:
insert image description here

uninstall package

You can run the npm uninstall command to uninstall the specified package:
insert image description here

Note: The uninstall package cannot be abbreviated as the command

After the npm uninstall command is successfully executed, the uninstalled package will be automatically removed from the dependencies in package.json.

devDependencies node

If some packages are only used in the project development phase and will not be used after the project is launched, it is recommended to record these packages in the devDependencies node. Correspondingly, if some packages need to be used after development and project launch, it is recommended to record these packages in the dependencies node.

You can log the package to the devDependencies node with the following command:

insert image description here

If you are not sure which node to put it in, when you download the package on the official website, it will prompt you to save it to which node.

Guess you like

Origin blog.csdn.net/zyb18507175502/article/details/124293236