spawn无法在node.js中启动全局包

时间:2016-03-22 07:59:15

标签: node.js

我需要通过另一个名为launcher的全局包将全局安装的节点包作为一个单独的进程启动。我希望启动器启动另一个包并退出。

spawn能够将记事本作为单独的进程启动。但无法启动全局安装的软件包。

我写了以下代码:

//var child = moduleLauncher.spawn('notepad',[],{

var child = moduleLauncher.spawn('hrm_module C:/test.scanRequest',[],{

           detached: true,

           stdio: ['ignore', out, err] 
});

child.unref();

生成错误:

events.js:154

  throw er; // Unhandled 'error' event
  ^

Error: spawn hrm_module C:/test.scanRequest ENOENT
    at exports._errnoException (util.js:856:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at nextTickCallbackWith2Args (node.js:475:9)
    at process._tickCallback (node.js:389:17)
    at Function.Module.runMain (module.js:449:11)
    at startup (node.js:140:18)
    at node.js:1001:3

注意:commond - hrm_module C:/test.scanRequest在Windows的命令提示符下工作正常。

我想通过启动器运行此命令。

2 个答案:

答案 0 :(得分:0)

spawn('hrm_module C:/test.scanRequest',[]

需要

spawn('hrm_module',['C:/test.scanRequest']

传递参数数组中的参数。否则,它将查找名为hrm_module C:/test.scanRequest的文件,但找不到您找到的文件。

答案 1 :(得分:0)

cmd为您启动模块:

const command = 'cmd';
const args = ['/C', 'hrm_module', 'C:/test.scanRequest']
const options = {/* your spawn options */ };
spawn(command, args, options);