gpt4 book ai didi

javascript - Node 子进程生成 "TypeError: Bad argument TypeError"?

转载 作者:行者123 更新时间:2023-11-29 21:11:44 30 4
gpt4 key购买 nike

当我用 Node 运行以下代码时:

var command = "/home/myScript.sh";
fs.exists(command, function(exists){
if(exists)
{
var childProcess = spawn(command, []); //this is line 602
}
});

我收到这个错误:

[critical error]: TypeError: Bad argument TypeError: Bad argument
at TypeError (native)
at ChildProcess.spawn (internal/child_process.js:274:26)
at exports.spawn (child_process.js:362:9)
at /home/simulations/GUIServer/Server/SocketServer.js:602:28
at FSReqWrap.cb [as oncomplete] (fs.js:212:19)
TypeError: Bad argument
at TypeError (native)
at ChildProcess.spawn (internal/child_process.js:274:26)
at exports.spawn (child_process.js:362:9)
at /home/simulations/GUIServer/Server/SocketServer.js:602:28
at FSReqWrap.cb [as oncomplete] (fs.js:212:19)

我的版本是:

  • Node 版本:v4.2.6
  • Express 版本:4.12.0

Linux x64 上运行。

文件权限为755:

-rwxr-xr-x 1 root root 2502 2016-12-06 17:12 myScript.sh

我正在以 root 身份运行。

关于什么可能导致此错误的任何想法?奇怪的是我认为它以前是有效的...

最佳答案

这是一个愚蠢的答案...“命令”未定义。原因如下:

我在循环的顶部有 var command,然后我稍后尝试重新定义 command。这不知何故导致 command 未定义。这是我正在做的:

错误

var command = "/home/myScript.sh";
fs.exists(command, function(exists){
if(exists)
{
//"command" here is UNDEFINED!?
var childProcess = spawn(command, []);
}else
{
var command = "something new"; //THIS IS WHAT CAUSED THE PROBLEM
}
});

正确

var command = "/home/myScript.sh";
fs.exists(command, function(exists){
if(exists)
{
var childProcess = spawn(command, []);
}else
{
command = "something new"; //FIXED
}
});

关于javascript - Node 子进程生成 "TypeError: Bad argument TypeError"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41516370/

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