Load and execute external js file in node.js with access to local variables?

Just do a require('./yourfile.js');

Declare all the variables that you want outside access as global variables.
So instead of

var a = "hello" it will be

GLOBAL.a="hello" or just

a = "hello"

This is obviously bad. You don’t want to be polluting the global scope.
Instead the suggest method is to export your functions/variables.

If you want the MVC pattern take a look at Geddy.

Leave a Comment