preface

Rare free time, today began to re-standardize the learning of Node programming. However, when I introduce the module, I see the mode of require, and then I think of our ES6 export and export default.

A xi bar, the head is big….

We’re done. Let’s sit down and figure out what they can do.

Require: The introduction of Node support

Export/import: Import exports supported only by ES6

Module. exports/exports: Exports supported only by Node

From this point on, I felt it was time to clarify the relationship between them, or I would die of confusion. Not much, let’s do it!!

The node module

The module system in Node follows the CommonJS specification. So what is 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

So let’s look at the code.

//utils.js
leta = 100; console.log(module.exports); {} console.log(exports); {} exports. A = 200; {a: 200} exports = {a: 200} exports = {a: 200} exports = {a: 200} exports ='Point to other memory area'; // exports = 0 // exports = 0 // exports = 0 //'/utils'); Console. log(a) // Prints 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.

Module. Exports only support module. Exports to operate data in memory, tired to death after all kinds of data operation, the result is really required out of the contents of Module.

But if you think about it in terms of a block of memory, it’ll make a lot of sense.

Then, to avoid confusion, try to export all module. Exports and then require imports.

Modules in ES export imports

Honestly, modules in ES are pretty clear. But there are a few details to clear up. Such as export and export default, and when importing, import a from.. ,import {a} from .. Anyway, it’s a little bit messy, so let’s start to sort it out.

The export and export default

Let’s start with these two derivations, and let’s talk about the differences between them

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

Let’s look at the code to verify this

testEs6Export.js

'use strict'// Export variablesexport const a = '100'; // Export methodexport const dogSay = function(){ 
    console.log('wang wang'); } // Export the second methodfunction catSay(){
   console.log('miao miao'); 
}
export { catSay };

//exportConst m = 100;export default m; 
//exportdefult const m = 100; // You can't write this format here.Copy the code

index.js

//index.js
'use strict'
var express = require('express');
var router = express.Router();

import { dogSay, catSay } from './testEs6Export'; / / is deducedexportThe method import m from'./testEs6Export'; / / is deducedexport default 

import * as testModule from './testEs6Export'; /* GET home page. */ router.get('/'.function(req, res, next) {
  dogSay();
  catSay();
  console.log(m);
  testModule.dogSay();
  console.log(testModule.m); // undefined because as exports are scatteredexportGather together as an object whileexportDefault is exported as the default attribute. console.log(testModule.default); // 100
  res.send('Congratulations, successful verification.');
});

module.exports = router;
Copy the code

As you can see from the above, it does feel like the ES6 module system is very flexible.

The code address

Making: github.com/XuXiaoGH/ex…

reference

Exports and module. Exports. Exports and exports,export and export default

Thank you to these three seniors for sharing.

Write in the last

If the article is helpful to you, please like it, it will be the motivation to support me to continue writing.

Thank you, folks.

Thanks to Humor on the 3rd floor for pointing out the error.