2021-10-07 learning record node modularization and package

Keywords: node.js


Modularization refers to the process of dividing the system into several modules from top to bottom when solving a complex problem. For the whole system, modules are units that can be combined, disassembled and replaced.

1. Reusability. 2. Maintainability. 3. Load on demand

Module classification

//1. Built in module
const fs =require('Module name')
//2. Custom module
const custom =require('Custom file path')
//3. Third party module (download into specified folder)
const fs =require('Module name')

Module scope

The variables, methods and other members defined in the user-defined module can only be accessed in the current module. This module level access restriction is called module scope. Prevent global variable contamination

Members in the scope of the outward sharing module

module.exports object

When using the require() method to import a module, the import result always depends on the object pointed to by module.exports (by default, exports and module.exports point to the same object)

Note: when using the require() method to import a module, the import result will always be subject to the object pointed to by module.exports.

exports object

Because the word module.exports is complex to write, Node provides exports object to simplify the code of sharing members externally. By default, exports and module. Exports point to the same object. The final shared result is based on the object pointed to by module.exports.

Modular specification

Which of LABjs, RequireJS and SeaJS is the best? Why- Know

  • commonjs specification: nodejs follows the commonjs specification.

  • ES6 modular specification: (a general modular specification for the front and rear ends; it can be used in Node.js, Vue and React!)

  • CMD and AMD modular specifications (rarely used): CMD--sea.js, AMD-require.js

  • UMD is called Universal Module Definition. It allows the same code module to run in projects using CommonJs, CMD or even amd through runtime or compile time. It does not have its own proprietary specifications, but integrates the specifications of CommonJs, CMD and AMD.

Package (third party module)

npm The world's largest package sharing platform

https://registry.npmjs.org/ Download the package you need from the server

//Installation package
npm install Package name (abbreviation: npm i (package name)
node_modules Folder is used to store all packages installed in the project. require() When importing a third-party package, you find and load the package from this directory.
package-lock.json Configuration files are used to record node_modules Download information of each package in the directory, such as package name, version number, download address, etc.
//Install the specified version
npm install Package name@Version number (abbreviation: npm i Package name@(version number)
Meaning of version number: the first digit-Large version, 2nd digit-Function version, 3rd digit-Bug Repair version
 Rules for version number promotion: as long as the previous version number increases, the subsequent version number returns to zero.

Package management configuration file - package.json

Project name, version number, description, etc., which packages are used in the project, which packages are only used during development, and which packages are required during development and deployment

npm init -y
//Download the package in package.json file!!! It can only run successfully in English directory
//In the package.json file, there is a dependencies node, which is used to record which packages you installed with the npm install command.
#Project package
dependencies node---#The core depends on the package developed and used online
npm install(npm i)---Install all packages at once
npm uninstall Package name---Delete specified package
devDependencies node ---#The development package depends on the package used in the development phase
npm i Package name -D ---Packages installed at development time
#Global package
npm i Package name -g //Globally installs the specified package
npm uninstall Package name -g //Uninstall globally installed packages
Only tool packages are necessary for global installation. Because they provide easy-to-use terminal commands.
To determine whether a package can be used only after global installation, you can refer to the official instructions.

Switch the lower package image source of npm

Method 1
npm config get registry//View the current package image source
//Switch the image source of the next package to Taobao image source
npm config set registry=https://registry.npm.taobao.org/
npm config get registry//Check whether the image source is downloaded successfully
Method 2
npm i nrm -g//Install nrm as a globally available tool through the npm package manager
nrm ls //View all available mirror sources
nrm use taobao//Switch the next package image source to taobao image source

Development package

① Create a new itheima tools folder as the root directory of the package ② in the itheima tools folder, create the following three files: ⚫ package.json (package management configuration file) ⚫ index.js (package entry file) ⚫ README.md (description document of package)

Initialize package.json

npm init//You can enter your own configuration when prompted
    "name":" itheima-tools",//Package name
    "version":"1.0.0",//Version number
    "main":"index.js",//Function code
    "description":"Provides formatting time, HTMLEscape Function of",//Brief description
    "keywords":["itheima","dataFormat","escape"],//Keyword search
    "license":"ISC"//license agreement

Modular splitting of different functions

const date = require('File location and file name')
const escape = require('File location and file name')

// Expose required members
module.exports = {

① Split functions into SRC - > separate files
② In index.js, import two modules to get the methods that need to be shared externally
③ In index.js, use module.exports to share the corresponding methods

Write documentation for the package
The README.md file in the package root directory is the instruction document for the package. Through it, we can write the instructions of the package in markdown format in advance, which is convenient for users to refer to.

Posted by saiko on Thu, 07 Oct 2021 15:27:27 -0700