Node.js global and process objects
In NW.js, the global
object works much like it does in Node.js with the peculiarity of being accessible from within the DOM, giving an additional way to share objects between the window and Node.js contexts. Moreover, when the window
object is created, all the members of the global
object are assigned to it, which is why you can use require()
from within the DOM.
This binding works both ways, so you can access global properties and methods assigned in the window context inside Node.js modules. Let's see a simple example:
index.html:
var gui = require('nw.gui'); gui.Window.open('newWindow.html'); global.sharedObjects = { myData: 'Foo' }; var myModule = require('./module.js');
module.js:
console.log(sharedObjects.myData);
newWindow.html:
console.log(global.sharedObjects.myData);
As illustrated, inside our module, we've omitted global
as it's already assigned as the global root variable. You may be tempted, then, to do all of your coding inside Node.js modules,...