Grunt registerTask()不在列表中运行任务

我已经添加了以下registerTask调用这个Gruntfile.js

 grunt.task.registerTask('runDebug', 'Run the app with debug flag', function() { var done = this.async(); grunt.util.spawn({ cmd: 'node', args: ['--debug', './node_modules/nodemon/nodemon.js', 'index.js'], opts: { stdio: 'inherit' } }, function (error, result, code) { if (error) { grunt.log.write (result); grunt.fail.fatal(error); } done(); }); grunt.log.writeln ('node started'); grunt.util.spawn({ cmd: 'node-inspector', args: ['&'], opts: { //cwd: current working directory } }, function (error, result, code) { if (error) { grunt.log.write (result); grunt.fail.fatal(error); } done(); }); grunt.log.writeln ('inspector started'); }); grunt.task.registerTask('debug', ['runDebug', 'compile', 'watch']); 

新的debug任务与现有的server任务类似。 但是, grunt server命令运行compilewatchrunNode任务,而grunt debug命令只运行runDebug任务。

我在这里错过了什么? 为什么不用grunt debug命令运行compilewatch任务。

你的代码this.async()调用this.async()返回的done()函数。 这可能会让Grunt感到困惑。 我会build议调用你自己的函数,它可以命名为spawned()而不是直接在callback函数中调用done() 。 函数可能是这样的:

 var expected_spawns = 2; function spawned() { if (!--expected_spawns) done(); if (expected_spawns < 0) throw new Error("too many spawns!") // Or some of Grunt's fail functions. } 

这样done()将被调用一次。