Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "module.exports" and "exports" in the CommonJs Module System

On this page (http://docs.nodejitsu.com/articles/getting-started/what-is-require), it states that "If you want to set the exports object to a function or a new object, you have to use the module.exports object."

My question is why.

// right module.exports = function () {   console.log("hello world") } // wrong exports = function () {   console.log("hello world") } 

I console.logged the result (result=require(example.js)) and the first one is [Function] the second one is {}.

Could you please explain the reason behind it? I read the post here: module.exports vs exports in Node.js . It is helpful, but does not explain the reason why it is designed in that way. Will there be a problem if the reference of exports be returned directly?

like image 740
Devs love ZenUML Avatar asked May 05 '13 10:05

Devs love ZenUML


People also ask

What is the difference between module exports and exports?

When we want to export a single class/variable/function from one module to another module, we use the module. exports way. When we want to export multiple variables/functions from one module to another, we use exports way. 2.

Is module exports CommonJS?

Although at first glance it may seem like module. exports , exports and require are global, actually they are not. CommonJS wraps your code in a function like this: (function(exports, require, module, __filename, __dirname) { // your code lives here });

How are ES modules different from CommonJS modules?

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.

What is the difference between CommonJS and ES6 modules?

While CommonJS and ES6 modules share similar syntax, they work in fundamentally different ways: ES6 modules are pre-parsed in order to resolve further imports before code is executed. CommonJS modules load dependencies on demand while executing the code.


1 Answers

module is a plain JavaScript object with an exports property. exports is a plain JavaScript variable that happens to be set to module.exports. At the end of your file, node.js will basically 'return' module.exports to the require function. A simplified way to view a JS file in Node could be this:

var module = { exports: {} }; var exports = module.exports;  // your code  return module.exports; 

If you set a property on exports, like exports.a = 9;, that will set module.exports.a as well because objects are passed around as references in JavaScript, which means that if you set multiple variables to the same object, they are all the same object; so then exports and module.exports are the same object.
But if you set exports to something new, it will no longer be set to module.exports, so exports and module.exports are no longer the same object.

like image 96
goto-bus-stop Avatar answered Oct 07 '22 17:10

goto-bus-stop