gpt4 book ai didi

javascript - 在 nwjs 中使用 shelljs 执行命令?

转载 作者:行者123 更新时间:2023-11-30 09:57:16 24 4
gpt4 key购买 nike

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

主要.js :

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

测试.sh :

echo "Hey there"

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

node main.js

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

[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)

我只想知道是否有任何解决方法或解决方案来执行代码。感谢您的帮助。

谢谢。

最佳答案

使用 test.sh 的完整路径:

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附近?你如何运行代码?从开发工具?从项目?来自打包项目?

此外,为什么需要 shelljs? Nwjs 已经有用于 shell 的内部 API:

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

关于javascript - 在 nwjs 中使用 shelljs 执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33458437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com