gpt4 book ai didi

node.js - 从nodejs应用程序运行MSI包

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:32 25 4
gpt4 key购买 nike

我想运行mongoDB来自 NodeJS 应用程序的 MSI 包。我试图遵循this的答案问题,但它给了我以下错误:

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 包的正确方法。

最佳答案

我建议在这种情况下使用spawn。 (请参阅 Node.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 的源代码.

关于node.js - 从nodejs应用程序运行MSI包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35365332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com