Tower Tower - 7 months ago 241
Javascript Question

How do you share constants in NodeJS modules?

Currently I'm doing this:


const FOO = 5;

module.exports = {

And using it in

var foo = require('foo');
foo.FOO; // 5

Is there a better way to do this? It feels awkward to declare the constant in the exports object.


You can explicitly export it to the global scope with global.FOO = 5. Then you simply need to require the file, and not even save your return value.

But really, you shouldn't do that. Keeping things properly encapsulated is a good thing. You have the right idea already, so keep doing what you're doing.