gpt4 book ai didi

node.js - NodeJS Spawn 不触发参数

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:59 25 4
gpt4 key购买 nike

我正在构建一个系统,它使用 spawn 来运行从文件读入的数组指定的命令。当我使用测试命令(例如 echo Something)时,一切工作正常,但是当我设置命令 npm installexpress -save 时,stdout 是如果您输入不带任何参数的 npm ,您会得到什么。

这里是处理命令的代码:

var runProcess = function (process, buildDir, callback) {
console.log('Running command: ' + process);
var args = process.split(" ");
var command = args[0];
var proc;
var stdout;
var stderr;

// Set arguments by shifting array
args.shift();

// Spawn the command
if (args.length) {
if (command === 'npm') {
proc = spawn(command, [args, "--color", "always"], {
cwd: buildDir
});
} else {
proc = spawn(command, [args], {
cwd: buildDir
});
}
} else {
proc = spawn(command, [], {
cwd: buildDir
});
}
}

如果在任何时候我console.log检查它是否进入正确的位置,它会正确跟踪,但输出始终只是npm命令转储与使用信息就好像它没有读取参数一样。

最佳答案

正如 @maxorq 所建议的那样,您的类型混淆了。您需要一个仅包含字符串的数组。您正在嵌套数组。试试这个:

  proc = spawn(command, args.concat( "--color", "always"), {
cwd: buildDir
});

关于node.js - NodeJS Spawn 不触发参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957166/

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