You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js child_process spawn传递含--的参数无法识别问题求解

问题根源

你遇到的参数识别问题核心是传参结构不符合wp-env run的命令要求:

  • wp-env run tests-cli "wp plugin update --all"中,被引号包裹的wp plugin update --all是单个独立参数,代表要在容器内执行的完整命令
  • 你现有代码把这部分拆成了wp/plugin/update/--all四个独立参数,wp-env只会把第四个参数wp识别为要执行的命令,后续参数会被误判为wp-env本身的参数,自然不会生效。
  • 你之前测试--version正常,是因为没有嵌套命令的场景,单参数传递符合npx的参数规则。
解决方案

推荐方案(关闭shell模式,更安全)

直接将容器内要执行的完整命令作为单个参数传入即可,不需要拆分:

const { spawn } = require("child_process");
 
const child = spawn('npx', [
    "wp-env",
    "run",
    "tests-cli",
    "wp plugin update --all"
]);
 
var scriptOutput = "";
 
child.stdout.setEncoding('utf8');
 
child.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
    data=data.toString();
    scriptOutput+=data;
});
 
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
    console.log('stderr: ' + data);
    data=data.toString();
    scriptOutput+=data;
});
 
child.on('close', function(code) {
    console.log('closing code: ' + code);
    console.log('Full output of script: ',scriptOutput);
});

兼容方案(保留shell: true)

如果必须开启shell模式,直接把整串命令作为第一个参数传入即可,不需要拆分参数数组:

const child = spawn('npx wp-env run tests-cli "wp plugin update --all"', {shell: true});
// 后续输出监听代码保持不变

这种写法相当于直接在shell环境执行完整命令,引号会被自动正确解析。

修改后运行即可正常触发插件全量更新逻辑,不会只停留在任务启动提示。

内容的提问来源于stack exchange,提问作者user16699981

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 11:45:00