tags: javascript webpack
When the front-end application becomes more and more complex, we want to divide the code into different modules for easy reuse and on-demand loading.
Require and import are statements that introduce modules under different modularization specifications. The differences between the two methods will be introduced below.
| years | Provenance | |
|---|---|---|
| require/exports | 2009 | CommonJS |
| import/export | 2015 | ECMAScript2015(ES6) |
What CommonJS loads is an object (the module.exports property), which will only be generated after the script is run. The ES6 module is not an object, and its external interface is just a static definition, which will be generated during the static code analysis phase. -Ruan Yifeng
If two files reference a module at the same time, when the value in the module is changed, the value in the module imported by require will not change, but the value in the module imported by import will change.
Usage of require/exports:
const fs = require('fs')
exports.fs = fs
module.exports = fs
The wording of import/export:
import fs from ‘fs’
import {default as fs} from 'fs'
import * as fs from 'fs'
import {readFile} from 'fs'
import {readFile as read} from 'fs'
import fs, {readFile} from 'fs'
export default fs
export const fs
export function readFile
export {readFile, read}
export * from 'fs'
| require/exports | import/export | |
|---|---|---|
| Node.js | All versions | Node 9.0+ (flag --experimental-modules must be added to start) Node 13.2+ (start directly)how to use? |
| Chrome | not support | 61+ |
| Firefox | not support | 60+ |
| Safari | not support | 10.1+ |
| Edge | not support | 16+ |
CommonJS modular scheme require/exports is designed for server-side development. Server module systemSynchronizeRead the content of the module file and get the module interface after compiling and executing. (Node.js is the implementation of the CommonJS specification).
On the browser side, because itsasynchronousThe feature of loading script file, CommonJS specification cannot be loaded normally. So there is a modular scheme designed by RequireJS and SeaJS (CommonJS compatible) for browsers.
Each of the two schemes has its own limitations, the following points need to be noted:
What is the difference between require and import? Different specifications to follow 1.require/exports is part of CommonJS 2.import/export is the new ES6 specification Different time of appearance Co...
When modifying vue&react and webpack, I often see require and jload in js file, both of which are used for JS modular programming (CSS is @import). Let's look at the difference between them. where...
1. require is the AMD specification, and import is the ES6 module. 2. require similar way of writing It seems to only introduce a, b, c, actually introduce the whole module, and then assign the corres...
Article directory The difference between `require` and `import` origin Export command / import command CommonJS es6 Static optimization requirewithimportDifference origin Before the birth of es6, js h...
The difference between require and import Before es6, js has not had its own module syntax. In order to solve this problem, there is a requirement for require.js. After the release of es6, js introduc...
Foreword Some time ago wrote an articleimport and require Depth - depth from shallow thinking to bring NodeThe articles comeimportis based onrequireImplementation, and implementation is different in d...
Before ES6, there was a bill to load JS modules, the most important being CommonJS and AMD specifications. The former CommonJS is mainly used in servers to achieve synchronous loading, such as nodejs....
First we have to understand the basic syntax of Require and Import: REQUIRE's basic syntax: Define module.export in the exported file, the type of object to export is not limited (which can be any typ...
Should I use require or import? The use of require is very simple, it is equivalent to the portal of module.exports, what is behind module.exports, what is the result of require, objects, numbers, str...
Both require and import are used for js modularization. First, require Require is the specification of commonjs. The node application is composed of modules and complies with the specification of comm...