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

CommonJS is a specification of Node.js and has been used ever since. Due to the large number of CommonJS libraries on npm and the differences between CommonJS and ES6, Node.js is not directly compatible with ES6. So at this stage require/exports is still necessary and necessary.

Different form

There are only three uses for require/exports:

const fs = require('fs');
 exports.fs = fs;
 module.exports = fs;
Copy code

Import/export is written in a variety of ways

import fs from 'fs';
import {default as fs} from 'fs';
import * as fs from 'fs';
-----------------------------
export default fs;
export const fs;
export * from 'fs';
Copy code

Essential difference

1. CommonJS or ES6 Module output can be seen as an object with multiple properties or methods;

2.default is a keyword unique to the ES6 Module. export default outputs the default interface object. Import from 'fs' can import this object directly.

3. The properties or methods of the imported module in the ES6 Module are strongly bound, including the underlying type; and CommonJS is the ordinary value passing or reference passing.

Intelligent Recommendation

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

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

More Recommendation

(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 modular require and import

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

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

[Import ... from "," import ... = require () "and" path: string) "What is the difference between" IMPORT (PATH: STRING)?

The first two are ES6 module syntax, the third import ... = Require () is the syntax of TS After defining the external interface of the module using the export command, you can load this module via th...

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

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

Top