Running Node app through Grunt

ε祈祈猫儿з 提交于 2019-11-27 17:46:07

问题


I am trying to run my Node application as a Grunt task. I need to spawn this as a child process, however, to allow me to run the watch task in parallel.

This works:

grunt.registerTask('start', function () {
  grunt.util.spawn(
    { cmd: 'node'
    , args: ['app.js']
    })

  grunt.task.run('watch:app')
})

However, when changes are detected by the watch task, this will trigger the start task again. Before I spawn another child process of my Node app, I need to kill the previous one.

I can't figure out how to kill the process, however. Something like this does not work:

var child

grunt.registerTask('start', function () {
  if (child) child.kill()
  child = grunt.util.spawn(
    { cmd: 'node'
    , args: ['app.js']
    })

  grunt.task.run('watch:app')
})

It appears that:

  1. Even though I store the spawned process in a variable outside of the function context, it does not persist, so the next time the start task is run, child is undefined.
  2. child has no kill function…

回答1:


This is because grunt-contrib-watch currently spawns all task runs as child processes. So the variable child is not within the same process context. Fairly soon, grunt-contrib-watch@0.3.0 will be released with a nospawn option. This will let you configure the watch to spawn task runs within the same context and would make your above example work.

Take a look at this issue for a little more information:

https://github.com/gruntjs/grunt-contrib-watch/issues/45




回答2:


Take a look at grunt-nodemon which handles a lot of the headaches related to spawning a child process.



来源:https://stackoverflow.com/questions/15044026/running-node-app-through-grunt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!