Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has already been answered but I wanted to add some clarification...</p> <p>You can use both <code>exports</code> and <code>module.exports</code> to import code into your application like this:</p> <p><code>var mycode = require('./path/to/mycode');</code></p> <p>The basic use case you'll see (e.g. in ExpressJS example code) is that you set properties on the <code>exports</code> object in a .js file that you then import using <code>require()</code></p> <p>So in a simple counting example, you could have:</p> <p>(counter.js):</p> <pre><code>var count = 1; exports.increment = function() { count++; }; exports.getCount = function() { return count; }; </code></pre> <p>... then in your application (web.js, or really any other .js file):</p> <pre><code>var counting = require('./counter.js'); console.log(counting.getCount()); // 1 counting.increment(); console.log(counting.getCount()); // 2 </code></pre> <p>In simple terms, you can think of required files as functions that return a single object, and you can add properties (strings, numbers, arrays, functions, anything) to the object that's returned by setting them on <code>exports</code>.</p> <p>Sometimes you'll want the object returned from a <code>require()</code> call to be a function you can call, rather than just an object with properties. In that case you need to also set <code>module.exports</code>, like this:</p> <p>(sayhello.js):</p> <pre><code>module.exports = exports = function() { console.log("Hello World!"); }; </code></pre> <p>(app.js):</p> <pre><code>var sayHello = require('./sayhello.js'); sayHello(); // "Hello World!" </code></pre> <p>The difference between exports and module.exports is explained better in <a href="https://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-nodejs">this answer here</a>.</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. 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