NodeJS 在JavaScript中执行命令时,子进程出现错误ENOENT

s4n0splo  于 2023-02-12  发布在  Node.js
关注(0)|答案(1)|浏览(178)

我想在我的package.json中用2个参数作为脚本来执行aws-vault。因此我创建了一个小助手脚本:
deploy.js

const { spawn } = require('child_process');
const args = process.argv.slice(2);

const child = spawn(
  `aws-vault exec ${args[0]} -- sls deploy --stage ${args[1]}`,
);

child.on('close', code => {
  console.log(`aws-vault process exited with code ${code}`);
});

在我的package.json中,脚本是这样定义的:

"scripts": {
    "start": "serverless offline --httpPort=4000",
    "deploy": "node deploy.js $@"}

但是当我运行yarn deploy myprofile mystage
我收到以下错误:

errno: -2,
  code: 'ENOENT',
  syscall: 'spawn aws-vault exec myprofile -- sls deploy --stage mystage',
  path: 'aws-vault exec myprofile -- sls deploy --stage mystage',
  spawnargs: []

我不确定我错过了什么...命令看起来正确

bybem2ql

bybem2ql1#

你可以设置这{ shell: true }并且再试一次.

const child = spawn(
  `aws-vault exec ${args[0]} -- sls deploy --stage ${args[1]}`,
  [],
  { shell: true }
);

相关问题