运行 来自 nodejs 应用程序的 MSI 包
Run MSI package from nodejs app
我想 运行 mongoDB MSI package from nodeJS application. I tried to follow the answer to 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 包的正确方式。
const dest = "cmd /c " + path.join(os.homedir(), fileName);
我建议在这种情况下使用 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 版本,它看起来会有点不同,请再次查看文档以获取更多信息,或者 post 另一个请求。
另请参阅 child_process.js.
的源代码
获取 运行 .msi 文件的最佳方法是使用 windows 提供的命令行工具,称为 msiexec。
所以截取的代码如下
import * as os from 'os';
import { spawn } from 'child_process';
// /quiet does installing in background process. Use docs to find more.
export const runMSI = (msiName: string, args?: string[]) => {
const childProcess = spawn('msiexec', [`/i ${msiName} /quiet`], {
detached: false,
cwd: os.homedir(),
});
childProcess.stdout.on('data', (data) => {
console.log('STDOUT: ', data.toString());
});
childProcess.stderr.on('data', (data) => {
console.log('STDERR: ', data.toString());
});
childProcess.on('close', (code: number) => {
console.log('Child exited with code ' + code);
});
};
我来自未来>>2020-jul-29。 nodejs 文档建议改用 exec
和 execFile
函数。
For convenience, the child_process module provides a handful of synchronous and asynchronous alternatives to child_process.spawn() and child_process.spawnSync(). Each of these alternatives are implemented on top of child_process.spawn() or child_process.spawnSync().
https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows
我想 运行 mongoDB MSI package from nodeJS application. I tried to follow the answer to 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 包的正确方式。
const dest = "cmd /c " + path.join(os.homedir(), fileName);
我建议在这种情况下使用 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 版本,它看起来会有点不同,请再次查看文档以获取更多信息,或者 post 另一个请求。 另请参阅 child_process.js.
的源代码获取 运行 .msi 文件的最佳方法是使用 windows 提供的命令行工具,称为 msiexec。 所以截取的代码如下
import * as os from 'os';
import { spawn } from 'child_process';
// /quiet does installing in background process. Use docs to find more.
export const runMSI = (msiName: string, args?: string[]) => {
const childProcess = spawn('msiexec', [`/i ${msiName} /quiet`], {
detached: false,
cwd: os.homedir(),
});
childProcess.stdout.on('data', (data) => {
console.log('STDOUT: ', data.toString());
});
childProcess.stderr.on('data', (data) => {
console.log('STDERR: ', data.toString());
});
childProcess.on('close', (code: number) => {
console.log('Child exited with code ' + code);
});
};
我来自未来>>2020-jul-29。 nodejs 文档建议改用 exec
和 execFile
函数。
For convenience, the child_process module provides a handful of synchronous and asynchronous alternatives to child_process.spawn() and child_process.spawnSync(). Each of these alternatives are implemented on top of child_process.spawn() or child_process.spawnSync(). https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows