1. Module / package classification
Node.js has three types of modules: built-in module, third-party module and user-defined module.
1.1 built in modules
The built-in module of Node.js is also called the core module. After Node.js is installed, it can be used directly. For example:
const path = require('path') var extname = path.extname('index.html') console.log(extname)
1.2 Node.js module of the third party
The third-party Node.js module refers to the module on npmjs.org published to realize some functions, which is used by the community according to a certain open source protocol. For example:
npm install chalk const chalk = require('chalk') console.log(chalk.blue('Hello world!'))
1.3 customized Node.js module
The custom Node.js module, also known as the file module, is a module written by ourselves for our own use. At the same time, when such modules are released to npmjs.org, they become open-source third-party modules.
The custom module is dynamically loaded at runtime. It requires complete path analysis, file location, compilation and execution process. The speed is slightly slower than that of the core module, but it is used very much.
1.3.1 module definition, interface exposure and reference interface
We can separate the public functions into a separate js file as a module. By default, the methods or properties in this module cannot be accessed outside. If you want the external to access the methods or properties in the module, you must expose the properties or methods in the module through exports or module.exports.
m1.js:
const name = 'gp19' const sayName = () => { console.log(name) } console.log('module 1') // Interface exposure method I: module.exports = { say: sayName } // Interface exposure method 2: exports.say = sayName // Wrong! exports = { say: sayName }
main.js:
const m1 = require('./m1') m1.say()
1.3.2 circular reference of module
Due to the incorrect use of exports, when two different js are referenced circularly, one js cannot obtain the method of the other js, resulting in an error. For example:
a.js
exports.done = false const b = require('./b.js') console.log('in a, b.done = %j', b.done) exports.done = true console.log('a done')
b.js
console.log('b starting') exports.done = false const a = require('./a.js') console.log('in b, a.done = %j', a.done) exports.done = true console.log('b done') main.js console.log('main starting') const a = require('./a.js') const b = require('./b.js') console.log('in main, a.done = %j, b.done = %j', a.done, b.done)
main.js will first load a.js, and then execute const b = require('. / b.js'); When, the program will transfer to loadb.js and execute const a = require('. / a.js') in b.js; To prevent infinite loops, return an incomplete copy of a.jsexports to the b.js module. Then b.js completes the loading and provides its export object to the a.js module.
We know that nodeJs has a layer of packaging for each JS file, which is called module. There is an attribute exports in the module. When you call require('a.js'), you actually return the module.exports object. Module.exports is initialized to an {} empty object. Therefore, in the above example, execute const a = require('. / A.js') in b.js; Instead of loading a new a module, the exports attribute of a module that has been loaded but has not been completed is returned to b module, so b.js gets the exports object of a module, that is, {done:false}. Although exports.done is modified to true in A.js, because A.js has not completed loading at this time, the attribute done of a module output in b.js is false, The attribute done of a module output in main.js is true. Nodejs solves the problem of circular reference by returning the unfinished exports object.
2, Differences between CommonJS and ES6 modularity
1. Create a folder with the following structure
2. File and folder resolution:
dist folder: used to store packaged files
src folder: used to store the source files we write
main.js: the entry file of the project. See the details below for details.
mathUtils.js: defines some math tool functions that can be referenced elsewhere and used. See the details below for details.
info.js: Several constants are defined
index.html: the browser opens the displayed homepage HTML
3. Code in mathUtils.js file: define two functions and export them in commonjs mode
function add(num1,num2) { return num1+num2 } function mul(num1,num2) { return num1*num2 } //CommonJS modular export module.exports={ add, mul }
4. info.js uses ES6 modularization to export constants
//ES6 modular export export const name="wangxiaoyu"; export const age=18; export const height=123;
5. main.js imports the above two file methods and constants
//commonJS modular import const math=require("./mathUtils.js") console.log("Hello Webpack"); console.log(math.add(10,20)); console.log(math.mul(10.20)); //ES6 modular import import {name,age,height} from "./info.js" console.log(name); console.log(age); console.log(height);