最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Execute command using shelljs in nwjs? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to execute a simple mand using shelljs in nwjs like this :

main.js :

var shell = require("shelljs");
var output = shell.exec("bash ./test.sh",{silent:true,async:false}).output;
console.log(output);

test.sh :

echo "Hey there"

When I run the above file in nodejs like this

node main.js

It works without any problems. But when I try to run the above code using nwjs(assuming we have the basic project structure setup with the index.html and main.js), it gives me an error.

[23874:1031/211359:INFO:CONSOLE(191)] ""shell.js: internal error"", source: node_modules/shelljs/src/mon.js (191)
[23874:1031/211359:INFO:CONSOLE(192)] ""Error: ENOENT: no such file or directory, open '/tmp/shelljs_b656f0ddaa7c3b096e97'\n    at Error (native)\n    at Object.fs.openSync (fs.js:540:18)\n    at Object.fs.readFileSync (fs.js:392:15)\n    at execSync (node_modules/shelljs/src/exec.js:109:24)\n    at Object._exec (node_modules/shelljs/src/exec.js:214:12)\n    at Object.exec (node_modules/shelljs/src/mon.js:182:23)\n    at file://main.js:33:16"", source: node_modules/shelljs/src/mon.js (192)

I just want to know if there is any work around or solution to execute the code. Help is appreciated.

Thank you.

I'm trying to execute a simple mand using shelljs in nwjs like this :

main.js :

var shell = require("shelljs");
var output = shell.exec("bash ./test.sh",{silent:true,async:false}).output;
console.log(output);

test.sh :

echo "Hey there"

When I run the above file in nodejs like this

node main.js

It works without any problems. But when I try to run the above code using nwjs(assuming we have the basic project structure setup with the index.html and main.js), it gives me an error.

[23874:1031/211359:INFO:CONSOLE(191)] ""shell.js: internal error"", source: node_modules/shelljs/src/mon.js (191)
[23874:1031/211359:INFO:CONSOLE(192)] ""Error: ENOENT: no such file or directory, open '/tmp/shelljs_b656f0ddaa7c3b096e97'\n    at Error (native)\n    at Object.fs.openSync (fs.js:540:18)\n    at Object.fs.readFileSync (fs.js:392:15)\n    at execSync (node_modules/shelljs/src/exec.js:109:24)\n    at Object._exec (node_modules/shelljs/src/exec.js:214:12)\n    at Object.exec (node_modules/shelljs/src/mon.js:182:23)\n    at file://main.js:33:16"", source: node_modules/shelljs/src/mon.js (192)

I just want to know if there is any work around or solution to execute the code. Help is appreciated.

Thank you.

Share asked Nov 1, 2015 at 1:45 dvenkatsagardvenkatsagar 9367 silver badges22 bronze badges 2
  • it seems like shelljs cant find a file, can you please try and run a global mand like ls? to see if the problem occurs then? – Alex Michailidis Commented Nov 1, 2015 at 9:54
  • @alex rokabilis , I did try the global option, and that also was giving an error, I even tried it in electron context instead of nwjs but that also did not work. The weird thing is that the child_process.execSync works but this doesn't. – dvenkatsagar Commented Nov 8, 2015 at 15:01
Add a ment  | 

1 Answer 1

Reset to default 5

Use full path to test.sh:

var shell = require("shelljs");
var output = shell.exec("bash /path/to/test.sh",{silent:true,async:false}).output;
console.log(output);

Looks like shelljs searching file in: /tmp/shelljs_b656f0ddaa7c3b096e97 Where you place test.sh? Near nwjs? How you run code? From devtools? From project? From packed project?

Also, why you need shelljs? Nwjs already have internall API to work with shell:

var nwGui = require('nw.gui')
    , nwShell = nwGui.Shell
    , child_process = require('child_process')
    , exec = child_process.exec
    , execSync = child_process.execSync
    , execFile = child_process.execFile
    , execFileSync = child_process.execFileSync
;

var output = execSync("bash /path/to/test.sh");
console.log(output);

https://github./nwjs/nw.js/wiki/shell

发布评论

评论列表(0)

  1. 暂无评论