node module 'nw.gui' not found

2019-01-27 02:51发布

问题:

This is the code I want to run

//global.$ = $;

var abar = require('address_bar');
var folder_view = require('folder_view');
var path = require('path');
var shell = require('nw.gui').Shell;

and this is what I get:

module.js:340 throw err; ^

Error: Cannot find module 'nw.gui'

at Function.Module._resolveFilename (module.js:338:15)

at Function.Module._load (module.js:280:25)

at Module.require (module.js:364:17)

at require (module.js:380:17)

at Object.<anonymous> (/home/parisa/Documents/nw-sample-apps-master/file-explorer/main.js:6:13)

at Module._compile (module.js:456:26)

at Object.Module._extensions..js (module.js:474:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:312:12)

at Function.Module.runMain (module.js:497:10) Program exited.

I can't get this module. what should I do?

回答1:

nw.gui is a NW.js (previously called node-webkit) module. NW.js should provide access to it when your code is run from within its runtime environment.

By the look of your error message, I assume you're running the file directly via NodeJS. To run a NW.js project you need to load it via the NW.js executable, which includes NodeJS. You can do this a few ways, as described in the "How-to-run-apps" page:

Find the project folder, which contains the package.json file. Either run it by zipping the whole folder up, changing the file extension to ".nw", and running the command:

nw /home/path/to/packagedapp.nw

Or, just run the command straight on the folder:

nw /home/path/to/appdir/

You can make a shortcut for this to make it easier. Eventually you can combine the NW.js executable with your code into a single executable, see How to package and distribute your apps.


Running it from inside node-webkit, still not finding 'nw.gui', any clue?

Maybe you're trying to access nw.gui from within the "Node context", and Node is complaining that it can't find it.

Javascript in NW.js can be run in Node context (which is like simply running the code within NodeJS, with all the NodeJS globals) or "Browser context" (which also has access to the browser, with the Window globals). Node context only has access to node stuff but the browser context has access to both.

Code that is included from a web page runs in browser context but code that is require()d gets executed in node context. See the document Differences of JavaScript contexts.



标签: node.js npm