簡體   English   中英

節點子進程執行命令失敗,錯誤代碼為 1

[英]Node Child Process Exec Command Failed with error code 1

我正在嘗試使用節點 js 子進程執行一些行並收到錯誤。 以下是我的代碼:

let cmd : string = "code " + PROJECTS[value];
exec(cmd, function callback(error, stdout, stderr) {
  console.log("started console app");
});

錯誤:

cmd:"C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-syn... (length: 82)"
code:1
killed:false
message:"Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\c... (length: 99)"
signal:null
stack:undefined

錯誤 JSON 的詳細信息。

Full CMD : "C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync""
Full message : "Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync"\n"

嘗試一個更簡單的例子..

var exec = require('child_process').exec;
var cmd = 'code C:\Program Files';
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout); 
});

這行得通嗎??

我不想看到整個錯誤(太冗長)所以我做了這樣的事情:

try { 
  const { stdout, stderr } = await exec('echo TEST');
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);
} catch (e) {
  // If exec fails and you want to see the whole ugly error: 
  // console.error(e);
  console.log('How about a nice human readable message instead?'); 
}

由於“等待”,這進入了“異步”功能。 更多信息: https ://stackoverflow.com/a/56095793/722796

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM