w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Is there a Node.js equivalent to PHP's include, so that included code has access to the parent file's variables?

The easiest way to share variables across modules is to assign the variables to the global namespace object. Variables declared how they would be globally declared in a browser are still module-specific in Node, so there is a global object.

Using the global scope is considered bad practice, but can be much simpler than other approaches:


global.num = 3;
global.str = 'a string';


console.log(num);  // 3
console.log(str);  // a string

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