从nodejs应用运行MSI包

我想从nodeJS应用程序运行mongoDB MSI包。 我试图按照这个问题的答案,但它给了我下面的错误:

internal/child_process.js:298 throw errnoException(err, 'spawn'); ^ Error: spawn UNKNOWN at exports._errnoException (util.js:837:11) at ChildProcess.spawn (internal/child_process.js:298:11) at exports.spawn (child_process.js:339:9) at exports.execFile (child_process.js:141:15) at C:\_PROJECTs\nodejs\automation\mongoDB-setup\auto-setup.js:34:5 at C:\_PROJECTs\nodejs\automation\mongoDB-setup\lib\file.js:31:5 at C:\_PROJECTs\nodejs\automation\mongoDB-setup\lib\file.js:20:5 at FSReqWrap.oncomplete (fs.js:82:15) 

当试图简单的EXE文件(例如puttygen.exe)它的作品。

这里是我有的代码的相关部分:

 'use strict' const os = require('os'), path = require('path'), setup = require('child_process').execFile; const fileName = 'mongodb.msi'; //const fileName = 'puttygen.exe'; const dest = path.join(os.homedir(), fileName); // run the installation setup(dest, function(err, data) { console.log(err); }); 

我不确定execFile是否也是MSI包的正确方法。

我build议在这种情况下使用spawn 。 (有关更多解释,请参阅节点js文档)。 在win64上,我认为你需要用参数产生命令行,否则child_process.js会为你做(对于unix来说)。

这里是你的案例(不是ES6)的例子:

 var os = require('os'), path = require('path'), setup = require('child_process').spawn; //1)uncomment following if you want to redirect standard output and error from the process to files /* var fs = require('fs'); var out = fs.openSync('./out.log', 'a'); var err = fs.openSync('./out.log', 'a'); */ var fileName = 'mongodb.msi'; //spawn command line (cmd as first param to spawn) var child = spawn('cmd', ["/S /C " + fileName], { // /S strips quotes and /C executes the runnable file (node way) detached: true, //see node docs to see what it does cwd: os.homedir(), //current working directory where the command line is going to be spawned and the file is also located env: process.env //1) uncomment following if you want to "redirect" standard output and error from the process to files //stdio: ['ignore', out, err] }); //2) uncomment following if you want to "react" somehow to standard output and error from the process /* child.stdout.on('data', function(data) { console.log("stdout: " + data); }); child.stderr.on('data', function(data) { console.log("stdout: " + data); }); */ //here you can "react" when the spawned process ends child.on('close', function(code) { console.log("Child process exited with code " + code); }); // THIS IS TAKEN FROM NODE JS DOCS // By default, the parent will wait for the detached child to exit. // To prevent the parent from waiting for a given child, use the child.unref() method, // and the parent's event loop will not include the child in its reference count. child.unref(); 

希望它有帮助:)如果你想要win32或UNIX版本,它会看起来有点不同,再次查看文档更多,或张贴另一个请求。 另请参阅child_process.js的源代码。

 const dest = "cmd /c " + path.join(os.homedir(), fileName);