Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between module.exports and exports in node

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the difference between module.exports and exports in node". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between module.exports and exports in node?"

Exports is a reference to module.exports, similar to the following

Const exports = module.exports

How will the following results be derived?

Module.exports = 100

Exports = 3

It is obvious that 100 will be exported, after all, exports is redirected.

"how is it implemented in the node source code? The essence of "exports" can be seen from the source code.

Module wrapper

For more information, please see the source code: https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L1252, which is consistent with the conjecture.

As we all know, all the module code in node is wrapped in this function.

(function (exports, require, module, _ _ filename, _ _ dirname) {

Exports.a = 3

});

And the following source code indicates how exports was obtained.

Const dirname = path.dirname (filename)

Const require = makeRequireFunction (this, redirects)

Let result

/ / you can see the essence of exports from here.

Const exports = this.exports

Const thisValue = exports

Const module = this

If (requireDepth = 0) statCache = new Map ()

If (inspectorWrapper) {

Result = inspectorWrapper (compiledWrapper, thisValue, exports

Require, module, filename, dirname)

} else {

/ / here is the module wrapper function

Result = compiledWrapper.call (thisValue, exports, require, module

Filename, dirname)

} at this point, I believe you have a deeper understanding of "what is the difference between module.exports and exports in node". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 227

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report