Node. js exports, module. exports, and ES6 export, export default in-depth explanation, node. jses6

Source: Internet
Author: User

Node. js exports, module. exports, and ES6 export, export default in-depth explanation, node. jses6

Preface

Recently, it is rare to have time. I decided to learn node programming again in a standardized manner. However, the introduction module I see uses the require method, and I think of our ES6 various export and export default.

Axi, The heads are all big ....

When the head size is over, let's take a look at their scope of use.

  • Require: Introduction supported by node and es6
  • Export/import: Only es6 supports export and import.
  • Module. exports/exports: Only nodes support export.

From this moment on, I think it is time to clearly understand the relationships between them, or else I will be confused. Not much. Let's do it !!

Node Module

The module system in Node follows the CommonJS specification.

Now again, what is the CommonJS specification?

Due to the chaos in the past, js does not have the concept of a module because each code is written. This specification is actually a definition of a module.

Modules defined by CommonJS include module, exports, and require)

First explain the exports and module. exports

When a node executes a file, it generates an exports and module object for the file,

The module has an exports attribute. For example, the relationships between them all point to a {} memory area.

exports = module.exports = {};


Let's take a look at the code.

// Utils. jslet a = 100; console. log (module. exports); // The output result is: {} console. log (exports); // The output result is: {} exports. a = 200; // hard work helps module. change the exports content to {a: 200} exports = 'point to other memory region'; // point exports to the specified path // test. jsvar a = require ('/utils'); console. log (a) // print as {a: 200}

From the above we can see that the content exported by require is actually the memory block content pointed to by module. exports, not exports.

In short, the difference between them is that exports is only a reference of module. exports, which assists the latter in adding content.

In the vernacular, exports only supports the module. the data in the exports operation memory is exhausted after a variety of operations. The final result is module. exports is really hard.

In fact, you can understand the concept of memory blocks.

To avoid confusion, use module. exports for export and then use require for import.

Module export and import in ES

To be honest, the module in es is very clear. However, some details need to be clarified.

For example, export and export default, and import a from..., import {a} from... are also messy during import, So let's begin to clarify them.

Export and export default

First, let's talk about the differences between the two exports.

  • Both export and export default can be used to export constants, functions, files, modules, etc.
  • In a file or module, there can be multiple export and import, and export default has only one
  • Export via export. Add {} during import, and export default is not required
  • Export can directly export the variable expression, but export default cannot.

Let's take a look at the code to verify it.

TestEs6Export. js

'Use strict '// export variable export const a = '000000'; // export method export const dogSay = function () {console. log ('wang wang ');} // export method second function catSay () {console. log ('miao miao');} export {catSay}; // export default export const m = 100; export default m; // export defult const m = 100; // This format cannot be entered here.

Index. js

// Index. js 'use strict 'var express = require ('express'); var router = express. router (); import {dogSay, catSay} from '. /testEs6Export '; // export the export method import m from '. /testEs6Export '; // export the export default import * as testModule from '. /testEs6Export '; // export the as set merging object/* GET home page. */router. get ('/', function (req, res, next) {dogSay (); catSay (); console. log (m); testModule. dogSay (); console. log (testM Odule. m); // undefined, because as export aggregates scattered exports as an object, and export default is exported as the default attribute. Console. log (testModule. default); // 100 res. send ('Congratulations, Verification Successful ');}); module. exports = router;

We can see from the above that the ES6 module system is indeed very flexible.

Code address

GitHub: https://github.com/XuXiaoGH/exportImportTest

Http://xiazai.jb51.net/201710/yuanma/exportImportTest (jb51.net).rar

References

1. Old tree sprout, use Express under ES6

2. Differences between exports and module. exports
3. Relationship between module. exports, exports, and export default

Thank you for sharing these three predecessors.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.