Nw.js: Cannot find module 'nw.gui' from node-main file

Created on 4 Jun 2014  路  6Comments  路  Source: nwjs/nw.js

I've added a node-main to my package.json and I'm trying to require nw-gui, but I'm getting a "Cannot find module 'nw.gui'" error. I'm not doing anything else in the js file and the index.html is a basic hello world example. Is this correct behaviour? And if so is there a workaround?

Most helpful comment

Sounds like a context "problem".

You need to use require from the correct context. Therefore, call window.require('nw.gui') in node-main. For example:

package.json:

{
  "name": "node-webkit", 
  "main": "index.html", 
  "node-main": "node-main.js"
}

index.html:

<h1>Sup</h1>

node-main.js:

setTimeout(function() {
  var gui = window.require('nw.gui');
  gui.Window.get().minimize();
}, 1000);

All 6 comments

Sounds like a context "problem".

You need to use require from the correct context. Therefore, call window.require('nw.gui') in node-main. For example:

package.json:

{
  "name": "node-webkit", 
  "main": "index.html", 
  "node-main": "node-main.js"
}

index.html:

<h1>Sup</h1>

node-main.js:

setTimeout(function() {
  var gui = window.require('nw.gui');
  gui.Window.get().minimize();
}, 1000);

Yep, that works. I do wonder though, in your example you're setting a timeout of 1 second before accessing the window. Is that for the minimize call or is it actually some requirement? It seems 0 milliseconds works as well.

Also, wouldn't it make sense to actually call the node-main script only once the window object is available? That way you wouldn't have to use a timeout to be able to access the window.

Is that for the minimize call or is it actually some requirement? It seems 0 milliseconds works as well.

No, this is not a requirement. I used setTimeout to keep the size of the snippets in my OP to a minimum. Using minimize was the shortest example that demonstrated interaction between the two contexts I could think of. In retrospect, perhaps not the best choice as I didn't explain my intentions clearly. Apologies for any confusion.

Also, wouldn't it make sense to actually call the node-main script only once the window object is available? That way you wouldn't have to use a timeout to be able to access the window.

Exactly. That's the right way to do this if you need node-main to interact directly with any windows. Expose a callback and trigger it from WebKit's context via process.mainModule on DOMContentLoaded, or whatever.

Actually, what I meant was, wouldn't it be useful for node-webkit to only initialize the node-main script once the window is already available? But your way works as well :)

Either way, I'll close this issue now. Thanks for clarifying.

Uhm not sure, but it's happening to me using window.

  var gui = window.require('nw.gui');
            ^
ReferenceError: window is not defined

My package.json looks like

"main": "index.html",
"node-main": "app.js",
"node-remote" : "*://index.html",

NWJS is nwjs-macappstore-v0.12.3-osx-x64.

same with me:

var gui = window.require('nw.gui');
          ^
ReferenceError: window is not defined

and package.json

...
"node-main": "./app.js",
"main": "http://localhost:3000",
...
Was this page helpful?
0 / 5 - 0 ratings