Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no problem creating it this way. Modules define what they return in the <code>module.exports</code> object. By the way, you don't actually need self executing functions (SEF), there is no global leakage like in browsers :-)</p> <h2>Examples</h2> <p>module1.js:</p> <pre><code>module.exports = { module: { 'version': '0.1.1' } }; </code></pre> <p>main.js:</p> <pre><code>var module1 = require( './module1.js' ); // module1 has what is exported in module1.js </code></pre> <hr> <p>Once you've understood how this works, you can easily export the version number right away if you want to:</p> <p>module1.js:</p> <pre><code>module.exports = '0.1.1'; </code></pre> <p>main.js:</p> <pre><code>var module1 = require( './module1.js' ); console.log( module1 === '0.1.1' ); // true </code></pre> <hr> <p>Or if you want some logic, you can easily extend your <code>module1.js</code> file like this:</p> <pre><code>module.exports = ( function() { // some code return version; } () ); // note the self executing part :-) // since it's self executed, the exported part // is what's returned in the SEF </code></pre> <hr> <p>Or, as many modules do, if you want to export some utility functions (and keep others "private"), you could do it like this:</p> <pre><code>module.exports = { func1: function() { return someFunc(); }, func2: function() {}, prop: '1.0.0' }; // This function is local to this file, it's not exported function someFunc() { } </code></pre> <p>So, in main.js:</p> <pre><code>var module1 = require( './module1.js' ); module1.func1(); // works module1.func2(); // works module1.prop; // "1.0.0" module1.someFunc(); // Reference error, the function doesn't exist </code></pre> <h2>Your special case</h2> <p>About your special case, I wouldn't recommend doing it like they're doing.</p> <p>If you look here: <a href="https://github.com/jasondavies/science.js/blob/master/science.v1.js" rel="noreferrer">https://github.com/jasondavies/science.js/blob/master/science.v1.js</a></p> <p>You see that they're not using the <code>var</code> keyword. So, they're creating a <strong>global variable</strong>.</p> <p>This is why they can access it once they <code>require</code> the module defining the global variable.</p> <p>And by the way, the <code>exports</code> argument is useless in their case. It's even misleading, since it actually is the <code>global</code> object (equivalent of <code>window</code> in browsers), not the <code>module.exports</code> object (<code>this</code> in functions is the global object, it'd be <code>undefined</code> if strict mode were enabled).</p> <h2>Conclusion</h2> <p>Don't do it like they're doing, it's a bad idea. Global variables are a bad idea, it's better to use node's philosophy, and to store the required module in a variable that you reuse.</p> <p>If you want to have an object that you can use in client side and test in node.js, here is a way:</p> <p>yourModule.js:</p> <pre><code>// Use either node's export or the global object in browsers var global = module ? module.exports : window.yourModule; ( function( exports ) { var yourModule = {}; // do some stuff exports = yourModule; } ( global ) ); </code></pre> <p>Which you can shorten to this in order to avoid creating the <code>global</code> variable:</p> <pre><code>( function( exports ) { var yourModule = {}; // do some stuff exports = yourModule; } ( module ? module.exports : window.yourModule ) ); </code></pre> <p>This way, you can use it like this on the client-side:</p> <pre><code>yourModule.someMethod(); // global object, "namespace" </code></pre> <p>And on the server side:</p> <pre><code>var yourModule = require( '../yourModule.js' ); yourModule.someMethod(); // local variable :-) </code></pre> <p>Just FYI, <code>..</code> means "parent directory". This is the relative path of where to get the module. If the file were in the same directory, you'd use <code>.</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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