last day (15 days later) » 

3:16 PM
0
A: understanding node.js exports/include

broofain file.js: module.exports = { health: 100, strengh: 87 } in other files: var enemy = require('./file'); // (or whatever the relative path to your file is More info here.

 
Where did module.exports come from? isn't it just exports?
 
module.exports is available to all modules, and allows you to redefine what object is exported by the module .vs. using the default exports module you get. It's often a simpler mechanism for exporting something in-bulk instead of hanging individual properties off `exports. (note, too, that it also allows you to export a function, string or other non-Object property should you choose to do that.)
 
I have exported functions and objects with just plain export.... I do not see the difference.
 
@neal - not enough room here to go into more detail. suffice it to say that you'll find some nice uses for it when you start creating sufficiently complex module dependencies.
 
I have... and I had to fall back to the 2nd part of what I put into my answer, if there was easier way, I would like to learn it...
I have been working with node since july. so i might have things mixed up
 
3:17 PM
Yup, there's an easier way. It's called module.exports. :)
 
but when I asked a question about it in the js room they gave me that second part of my answer
So how do I use it?
 
Every module has an exports object by default, which is shared across all requiring modules. As long as you modify that object you're fine.
So either hang your properties directly off that, or export the 'enemy' object your creating directly, using module.exports.
does that make sense?
 
@broofa remember I am not the op of that question :-P
I have something like this:
 
I know. but I don't think this discussion is necessarily of interest to the op.
 
  Object.defineProperty(exports, "updates", {
    get: function() {
      return updates;
    }
  });
where updates is a socket.io socket
 
3:19 PM
Yeah, fuck that Object.defineProperty stuff... it's only useful in cases where you want to prevent requiring modules from modifying properties. :)
 
updates = io.of('/updates').on('connection', function ...
So how would i send the current state of update to the other modules without the defineProperty part?
 
If you want to export a module object where changes to properties on that object are seen by requiring modules, you can do one of two things...
var mything = {blah...}
module.exports = mything
(.. then, later ...)

mything.foo = new_value; // seen everywhere
or two ...
exports.foo = 123;

(then, later ...)

exports.foo = 345; // also seen everywhere.
 
ahhh so modules.exports is king of a "reference"?
but that means I can only have one thing in the module.exports?
 
urr... sure, if that's how you want to describe it. :)
 
then how do I call it in the other modules?
 
3:22 PM
call what?
Let's say the above code is in baz.js, I guess ...
 
@broofa my updates obj?
 
var thing = require('baz');
console.log(thing.foo); // will output current value of whatever foo property is set to in the original module.
 
wait.... module.exports just gives mything the same power as exports.... then what the heck in the difference?
 
I think you're overcomplicating this. :)
 
that does not help me at all.
I have multiple objects in my modules that change all of the time
 
3:24 PM
if all you're exporting is a generic Object, then there arguably isn't much difference.
 
and I need access to them in other modules in their current state
 
but if, say, you want to export something else ...
 
so if I set:
exports.updates = updates;
it will only be equal to updates in THAT state
 
module.exports = function() {...}
 
not in any future state!
 
3:26 PM
var myFunction = require('./baz');
myFunction(); // calls exported function.
 
oy... you obviously do not understand that objects can change
and I do not want to expose everything to the other modules. yes?
 
Uhm, no. I'm not the one who's confused here... or, at least, not making sense.
 
but I still want the other modules to be able to GET the information
 
Okay, so you never mentioned "read-only" access, which is a different ball of wax.
I gotta get to work and, frankly, I think at this point you should just go write some code and experiment. There's not much more I can do here.
 
@broofa can you come to the JS room?
..
 
3:28 PM
Nope - gotta get to work.
Sorry if I'm coming across as a bit abrupt/rude. I really do hope this has been helpful.
Cheers!
 
@broofa I do not think you understand what I am doing in my code
it has nothing to do with the OP
 
At this point that's your problem, not mine. ;)
 
but there is no reason to downvote my answer since it is correct.
your answer is completely diff implementation... @broofa
 
 
3 hours later…
6:33 PM
@Neal The first half of your answer is correct, yes. But there was nothing about the OP's question that hinted at needing read-only access to these properties. Worse, you didn't explain that this was the purpose of your code, which leaves readers struggling to understand how that side point applies to the OPs question. Ergo, it's confusing and irrelevant. hence, down vote. Sorry.
 

last day (15 days later) »