[英]Node child_process.spawn bad file descriptor on windows with ipc
[英]How to ensure node.js child_process.spawn will write error messages to the console
我在運行子進程並將其輸出寫入控制台時遇到很多麻煩。 在此情節中,我嘗試使用spawn
運行Windows mklink
命令。 錯誤是我無權寫入文件。
但是, 我的問題是該錯誤沒有以任何方式告訴我。
以下打印You do not have sufficient privilege to perform this operation.
到控制台:
mklink /DC:\\some\\path\\to\\my\\intended\\link C:\\path\\to\\my\\folder
但是在node.js中運行它只會給我Error: spawn ENOENT
這是非常無用的錯誤消息:
require('child_process').spawn('mklink',
['/D', 'C:\\some\\path\\to\\my\\intended\\link',
'C:\\path\\to\\my\\folder'],
{stdio:'inherit'})
盡管有stdio:“繼承”,我在控制台上什么也沒得到。 我也嘗試了以下方法:
var x = require('child_process').spawn('mklink',
['/D', 'C:\\some\\path\\to\\my\\intended\\link',
'C:\\path\\to\\my\\folder'])
x.stdout.pipe(process.stdout)
x.stderr.pipe(process.stderr)
但是沒有骰子。 根本沒有控制台輸出。 請注意,我確實使用exec
獲得控制台輸出:
var x = require('child_process')
.exec('mklink /D C:\\some\\path\\to\\my\\intended\\link C:\\path\\to\\my\\folder')
x.stdout.pipe(process.stdout)
x.stderr.pipe(process.stderr)
這不需要有關Windows mklink的工作原理的任何特殊知識-我的問題很簡單,就是使用node.js spawn
進行錯誤報告。
我在這里做錯了什么? 這是node.js中的錯誤嗎?
更新 :似乎此錯誤已由節點v0.10.29修復
對我而言, stdio
無法正常工作。
嘗試這個:
// Helper function to execute and log out child process
// TODO: implement a better success/error callback
var spawnProcess = function(command, args, options, callback) {
var spawn = require('child_process').spawn;
var process = spawn(command, args, options),
err = false;
process.stdout.on('data', function(data) {
grunt.log.write(data);
});
process.stderr.on('data', function(data) {
err = true;
grunt.log.errorlns(data);
});
if (typeof callback === 'function') {
process.on('exit', function() {
if (!err) {
return callback();
}
});
}
};
spawnProcess('mklink', ['/D', 'C:\\some\\path\\to\\my\\intended\\link', 'C:\\path\\to\\my\\folder'], {}, done);
作為一種解決方法,請嘗試以下操作:
require('child_process').spawn('cmd',
['/C', 'mklink', '/D', 'C:\\some\\path\\to\\my\\intended\\link',
'C:\\path\\to\\my\\folder'],
{stdio:'inherit'})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.