Reputation: 26778
I've recently been learning about Node import systems.
I know how Node's module.exports
can be used:
// file_1
var MyClass = module.exports = {}
// file_2
var MyClass = require("./file_1.js")
and how I can alternatively use globals
// file_1
global.MyClass = {foo: "bar"}
// file_2
require("./file_1.js")
console.log(MyClass.foo)
I prefer the globals approach personally, but I've been told that it's good practice to also export a class in my files.
The thing is, my main file doesn't really have any class of its own right now. All the classes are in files which it requires, and all the app's components are present on the global
object.
I'm not really sure there's a benefit to using module.exports
on my main file. I thought than an easy way to say my files exported something would be to simply export the entire global
object. Since this object is probably a bit large, though, I worry that there would be performance degradation for a feature I'm not sure is useful.
Is there any point to using module.exports
if I am defining my modules on the global
object?
Upvotes: 0
Views: 62
Reputation: 665020
I prefer the globals approach personally, but I've been told that it's good practice to also export a class in my files.
I'm sure you've been told that using globals is a bad practice, and that you should export your classes via module.exports
instead. Not additionally.
I'm not really sure there's a benefit to using
module.exports
on my main file.
No, your main file (which is typically executed as a script, not a module) doesn't need to export anything. Just leave it away. Use local variables, or create some globals if you must.
Upvotes: 1