Nodemon-like task in Grunt : execute node process and watch

前端 未结 3 1427
执笔经年
执笔经年 2021-02-03 22:49

I feel like I\'m missing something.

Here is what I want to achieve :

Having a grunt task that executes my server.js and runs watch task

3条回答
  •  余生分开走
    2021-02-03 23:03

    Hi I also came across this problem and here is my solution (based on nackjicholson's answer). This uses grunt-nodemon in a spawned process. so I can:

    • Reload nodejs
    • Watch for changes to e.g. .less files
    • Get output of both tasks

      grunt.loadNpmTasks('grunt-nodemon');
      grunt.initConfig({
          nodemon: {
              dev: {
                  options: {
                      file: 'server.js',
                      nodeArgs: ['--debug'],
                      env: {
                          PORT: '8282'
                      }
                  }
              }
          },
      });
      
      grunt.registerTask('server', function (target) {
          // Running nodejs in a different process and displaying output on the main console
          var nodemon = grunt.util.spawn({
               cmd: 'grunt',
               grunt: true,
               args: 'nodemon'
          });
          nodemon.stdout.pipe(process.stdout);
          nodemon.stderr.pipe(process.stderr);
      
          // here you can run other tasks e.g. 
          // grunt.task.run([ 'watch' ]);
      
      });
      

提交回复
热议问题