nodejs - exporting "variable" var

node.js

Solution

As mentioned in my comment, I would just attach `myNumber` to `module.exports`:

module.exports.myNumber = 0;

setInterval(myMethod, 1);

function myMethod() {
    module.exports.myNumber++;
}

You can update your exported object as much as you want because other modules that `require` your module will share the same reference as `module.exports` in your module code.

Problem

I have two files in a node.js project; timer.js and app.js. Basically what timer does is, it has a variable `myNumber` which increases with a `setInterval` function. I would like to be able to access that variable whenever I need it, currently I am using the following code: ``` var myNumber = 0; setInterval(myMethod, 1); function myMethod() { myNumber++; module.exports = myNumber; } ``` and I got the feeling this is very wrong (I am a complete newbie), can anyone enlighten me on how to do it the right way? What I'd like to do is that every time I am getting a variable like this in another file, I get the current value of myNumber. Currently I am doing it this way, and it's working (still, this must be wrong): ``` exports.index = function(req, res){ var timer = require("../timer.js"); res.end("timer tick at: " + timer); }; ```

Original source