首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用nwjs中的shelljs执行命令?

使用nwjs中的shelljs执行命令?
EN

Stack Overflow用户
提问于 2015-11-01 01:45:57
回答 1查看 5.8K关注 0票数 2

我试图使用nwjs中的shelljs执行一个简单的命令,如下所示:

main.js:

代码语言:javascript
复制
var shell = require("shelljs");
var output = shell.exec("bash ./test.sh",{silent:true,async:false}).output;
console.log(output);

test.sh:

代码语言:javascript
复制
echo "Hey there"

当我像这样在nodejs中运行上面的文件时

代码语言:javascript
复制
node main.js

它的工作没有任何问题。但是当我尝试使用nwjs运行上面的代码时(假设我们使用index.html和main.js设置了基本的项目结构),它会给我一个错误。

代码语言:javascript
复制
[23874:1031/211359:INFO:CONSOLE(191)] ""shell.js: internal error"", source: node_modules/shelljs/src/common.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/common.js:182:23)\n    at file://main.js:33:16"", source: node_modules/shelljs/src/common.js (192)

我只想知道是否有任何工作或解决方案来执行代码。我们很感激你的帮助。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-11 12:49:57

使用test.sh的完整路径:

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

看起来像shelljs搜索文件: /tmp/shelljs_b656f0ddaa7c3b096e97,您把test.sh放在哪里?在nwjs附近?你是怎么运行代码的?从devtools来的?从项目中?从拥挤的项目?

还有,你为什么需要贝壳?Nwjs已经有了可以使用shell的internall:

代码语言:javascript
复制
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.com/nwjs/nw.js/wiki/shell

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33458437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档