简体   繁体   English

尝试在Windows上使用Gulp和Babel来分叉Node.js进程

[英]Trying to fork a Node.js process using Gulp and Babel on Windows

My application is built with isomorphic JavaScript, and I'm using a Gulp task to run a Node.js server on a new instance, then listening to files change to make a server restart and a browser reload (using Browsersync ). 我的应用程序是使用同构JavaScript构建的,我正在使用Gulp任务在新实例上运行Node.js服务器,然后监听文件更改以使服务器重新启动并重新加载浏览器(使用Browsersync )。

I want to use Babel to write my server-side code in ECMAScript 6, as well as my client-side code. 我想使用Babel在ECMAScript 6中编写我的服务器端代码,以及我的客户端代码。 I want to avoid the Babel polyfill and use node_modules/.bin/babel-core.cmd server.js command instead, but child_process.fork() allows only an executable: 我想避免使用Babel node_modules/.bin/babel-core.cmd server.js并使用node_modules/.bin/babel-core.cmd server.js命令,但是child_process.fork()只允许执行:

let child = cp.fork('server.js', [], {
  execPath: path.normalize('node_modules/.bin/babel-node.cmd'),
  env: _.assign({NODE_ENV: 'development'}, process.env)
});

This script doesn't work, and as intended, throws the following error: 此脚本不起作用,并按预期抛出以下错误:

child_process.js:588
  p.open(fd);
    ^
Error: EINVAL, invalid argument
    at Error (native)
    at Object.exports._forkChild (child_process.js:588:5)
    at Function.startup.processChannel (node.js:704:10)
    at startup (node.js:59:15)
    at node.js:814:3

I want to use child_process.fork() to this case. 我想在这种情况下使用child_process.fork() child_process.spawn() doesn't work here because there is not a direct communication between the two Node.js processes, indeed. child_process.spawn()在这里不起作用,因为实际上两个Node.js进程之间没有直接通信。 And I'm listening from events to performs actions: 我正在从事件中听取执行操作:

// server.js

server.listen(port, function() {
  if (process.send) {
    process.send('online');
  } else {
    ...
  }
});

// gulpfile.babel.js

child.on('message', (message) => {
  if (message.match(/^online$/)) {
    if (browserSyncServer) {
      browserSyncServer.reload();
    }

    if (!started) {
      started = true;

      gulp.watch(config.serverScripts, () => {
        plugins.util.log('Restarting development server.');

        server.kill('SIGTERM');
        server = startup();
      });

      cb();
    }
  }
});

Any workaround with this? 这有什么解决方法吗? Or I'm forced to use the Babel polyfill directly on my server-side code? 或者我被迫直接在我的服务器端代码上使用Babel polyfill?

You can change the command that it uses to spawn the node process with the execPath option. 您可以使用execPath选项更改它用于生成节点进程的execPath

For example, if I want to spawn a node server with babel-node instead of node , I can type: 例如,如果我想生成一个带有babel-node而不是node的节点服务器,我可以输入:

process.fork('/path/to/server.js', { execPath: "babel-node" }, function(error, stdout, stderr) {
  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);

  if (error !== null) {
    console.log('exec error: ' + error);
  }
});

I had this problem was well. 我有这个问题很好。 With this issue I open on mocha I could solve the problem. 有了这个问题,我在摩卡上打开我可以解决问题。

Running babel-node node_modules/mocha/bin/_mocha test.js applies by default the compiler to the child process and there is no need to set the execPath . 运行babel-node node_modules/mocha/bin/_mocha test.js默认情况下将编译器应用于子进程,并且不需要设置execPath

PS: Once you run the mocha with babel-node there is no need to pass --require or --compilers . PS:一旦你用babel-node运行mocha ,就不需要传递--require--compilers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM