Require: imports supported by both Node and ES6

Export, export default/import: only ES supports import and export

Module. exports/exports: Exports supported only by Node

The export and export default

  • There can be more than one export, import, and only one export default
  • Use the export method to export data. {} is required when importing data. Export default is not required
  • Export can directly export variable expressions, but export default cannot
  • When exporting export default, you can customize the names of imported variables
  • Export, export default can export variables, functions, and objects

1. The use of the export

a.js
export const a = 100;

export const func = function() { console.log(func) };

const b = 200;
const func2 = function() {console.log(func2)}

export{b, func2} // passexportMethod export, import with {}Copy the code

2. Use of export default

B.j s error:export// Default is a special system variable,exportThe meaning of default is to assign the variable following this command to the special system variable default and export it to other modules for use. Correct: cons a = 11export default a

Copy the code

3. The use of the import

1. import {a, b} from './a.js'

2. import * as aModule from './a.js'

3. import bb from './b.js'// where bb can be any self-defined variable nameCopy the code

Exports and the module exports

When nodeJS executes a file, export and module.exports objects are generated

exports = module.exports = {}

Require imports the contents of modules pointed to by module.exports