Node Child Process Spawn“TypeError:Bad argument TypeError”?

时间:2017-01-07 00:09:39

标签: javascript node.js express

当我使用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)

我的版本是:

  • 节点版本:v4.2.6
  • Express Version:4.12.0

Linux x64 上运行。

该文件的权限是755:

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

我正在以 root 运行。

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

1 个答案:

答案 0 :(得分:2)

这是一个愚蠢的答案...... "命令"未定义 。这就是原因:

我的循环顶部有var command,之后我尝试重新定义command。这在某种程度上导致command未定义。这就是我在做的事情:

<强> WRONG

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
    }
});