Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My guess is the original author of that sample code is confused about <code>module.exports</code> vs <code>exports</code>. To use the <code>exports</code> object, you must add properties to it like this:</p> <pre><code>exports.size = 42; </code></pre> <p>If you re-assign the <code>exports</code> <em>variable</em> to a new object, you basically lose access to the global exports object that node.js provided for you. If you do this twice or three or N times, the effect is the same. It's useless. For example: mod_b.js</p> <pre><code>var realExports = exports; realExports.height = 42; var exports = {}; exports.weight = 43; </code></pre> <p>And in mod_a.js</p> <pre><code>var mod_b = require('./mod_b'); console.dir(mod_b); </code></pre> <p>Run <code>node mod_a.js</code> and you get:</p> <pre><code>{ height: 42 } </code></pre> <p>Notice <code>height</code> is there but <code>weight</code> is not. Now, what you <em>can</em> do is assign <code>module.exports</code> to be an object, and that is the object that will be returned when another module <code>require</code>s your module. So you will see things like.</p> <pre><code>var x = 10; var y = 20; module.exports = {x: x, y: y}; </code></pre> <p>Which will do what you expect. Here's some informative articles on the details.</p> <p><a href="http://www.hacksparrow.com/node-js-exports-vs-module-exports.html" rel="nofollow noreferrer">Node.js Module – exports vs module.exports</a></p> <p><a href="https://stackoverflow.com/q/5311334/266795">What is the purpose of NodeJS module.exports and how do you use it?</a></p> <p><a href="http://visionmedia.github.com/masteringnode/book.html" rel="nofollow noreferrer">Mastering Node</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload