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.