The difference between modular require and import

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.

1. The time and place of appearance are different

years Provenance
require/exports 2009 CommonJS
import/export 2015 ECMAScript2015(ES6)

2. Require/exports is dynamically loaded at runtime, and import/export is statically compiled

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

3. The output of require/exports is a copy of the value, and the output of the import/export module is a reference of the value

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.

4. Inconsistent usage

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'

5. Restrictions on the use of different terminals (client/server)

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+

6. Alternatives

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:

  • Native browsers do not support require/imports. You can use packaging tools such as Browsersify and webpack that support CommonJS module specifications to package code.
  • Import/export cannot be used directly in the browser, we need to add the type="module attribute to the <script> element of the imported module.
  • Even though Node.js 13.2+ already supports import/export, Node.js officially does not recommend using it in a formal environment. It can be used currentlybabel Compile the ES6 module system into the CommonJS specification (note: the syntax is the same, but the specific implementation is still require/exports).

7. Reference

  • Ruan Yifeng-Introduction to ECMAScript 6
  • Cunzhi-knowing the answer-require, import difference
  • From CommonJS to Sea.js
  • sunshine-front-end modularity (AMD, CommonJS, UMD) summary
  • Oak Tree-JavaScript Modularization Summary
  • Zhang Xinxu-Hurray, the browser natively supports ES6 export and import modules!

Intelligent Recommendation

What is the difference between require and import?

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...

Difference between require and import in JavaScript

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...

Analysis of the difference between import and require

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...

[js] difference between require and import

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 in vue

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...

More Recommendation

The difference between the import and require the webpack

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...

(Transfer) The difference between import and require

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....

The difference between the Require and Import of Vue

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...

The difference between import module require and import in node

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...

What is the difference between js using require and import?

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top