Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

node.js - node module 'nw.gui' not found

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...