Using range

  • The introduction of require: node and ES6 support
  • Export/import: Import exports supported only by ES6
  • Module. exports/exports: Exports supported only by Node

The node module

The module system in Node follows the CommonJS specification. Because JS is more chaotic in the past, each to write each code, there is no concept of a module, and this specification is actually a definition of the module.

CommonJS modules include module identifiers, module definitions, and module references.

Exports and module. When a node executes a file, exports and module objects are generated for the file. Module has an exports property. The relationship between them is shown below, pointing to a {} memory region.

exports = module.exports = {};
Copy the code
//utils.js
let a = 100;

console.log(module.exports); // Can print the result: {}
console.log(exports); // Can print the result: {}

exports.a = 200; // Exports module. Exports {a: 200}

exports = 'Point to other memory area'; // Exports are exports

//test.js

var a = require('/utils');
console.log(a) // Print as {a: 200}
Copy the code

Module. Exports exports: module. Exports exports: module. Exports: module. Exports is only a reference to module.exports, which assists the latter in adding content.

Modules in ES export imports

Export and Export default First let’s talk about these two exports, and let’s talk about the differences between them

  • Export and export default can be used to export constants, functions, files, and modules
  • In a file or module, there can be more than export and import, but only one export default
  • If you export the file in export mode, add {} when importing the file. Export default does not need to import the file
  • Export can export variable expressions directly, export default cannot.

reference