w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Require files with scope

If you're wanting to pass it into the module then you can do it like this.

var scope = {
    message: "Hello World"
};

require('./echo.js')(scope);

echo.js:

module.exports = function(obj){
    console.log(obj.message);
}

It's the only way unless you declare scope a global variable. Not recommended due to polluting the global namespace.

global.scope = {
    message: "Hello World"
}
require('./echo.js');

echo.js:

console.log(global.scope.message);




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.