与Grunt同时运行`watch`&`nodemon`

我刚刚开始使用Grunt,并希望运行grunt-contrib-watch [GitHub页面]在每次修改文件时使用我的JavaScript(使用grunt-contrib-jshint [GitHub页面] )并运行grunt-nodemon [ GitHub页面] ,同时使用grunt-concurrent [GitHub页面] 。

据我所知(我显然不)我的Gruntfile应该:

  1. 默认运行concurrent
  2. concurrent运行watch
  3. 每次修改文件时, watch都会运行jshint

Gruntfile.js

 module.exports = function (grunt) { grunt.initConfig({ concurrent: { dev: [ 'watch' ], options: { logConcurrentOutput: true } }, jshint: { server: [ '**/*.js', '!node_modules/**/*.js' ], options: { node: true } }, watch: { all: [ '**/*/.js', '!node_modules/**/*.js' ], tasks: [ 'jshint' ] } }); grunt.loadNpmTasks('grunt-concurrent'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('default', [ 'concurrent:dev'/*, 'jshint', 'watch'*/ ]); }; grunt.loadNpmTasks('grunt-concurrent'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('default', [ 'concurrent:dev' ]); }; 

NB我还没有joingrunt-nodemon

它看起来像concurrent正在运行watch但是当我修改文件看起来jshint没有运行。 我当然不会在terminal中得到任何输出(我认为logConcurrentOutput: true是这样做的)。

这是我在terminal得到的输出:

 Running "concurrent:dev" (concurrent) task Running "watch" task Waiting... Done, without errors. 

我也想运行jshint当我第一次运行default任务(以及当我修改文件)。

任何人都可以告诉我哪里出错了?

谢谢!

如果没有发现“监视”文件, watch任务将退出; 根据这个问题 。

为了方便地告诉watch观看与jshint任务相同的文件,我使用Grunt的模板引擎来引用与jshint任务相同的文件Array

然后,我将jshint添加到并发任务列表中运行,以便在最初运行时以及在修改文件(使用watch )时运行。

这是我的工作Gruntfile

 module.exports = function (grunt) { grunt.initConfig({ concurrent: { dev: [ 'jshint', 'watch' ], options: { logConcurrentOutput: true } }, jshint: { server: [ '**/*.js', '!node_modules/**/*.js' ], options: { node: true } }, watch: { files: '<%= jshint.server %>', tasks: [ 'jshint' ] } }); grunt.loadNpmTasks('grunt-concurrent'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('default', [ 'concurrent' ]); }; 

乔纳森你需要检查你的通配符模式实际上是否匹配任何文件。 作为问题的链接build议你可以尝试nonull选项。 我也会build议用标志运行咕噜声 – 因此你可以看到更多的引擎盖下发生了什么。

这是一个工作的Gruntfile ,它启动一个节点服务器,监视更改和实时重新载入。 它使用grunt-express插件。

如果你正在运行一个Express服务器,那么你可以使用grunt-express-server 。 该文档有一个很好的使用JSHINT和LiveReload + Watch / Regarde的指导。

 grunt.initConfig({ jshint: { all: ['Gruntfile.js', 'public/javascripts/*.js', 'test/**/*.js'] }, watch: { express: { files: ['**/*.js', '**/*.ejs'], tasks: ['jshint', 'express:dev'], options: { spawn: false } } }, express: { dev: { options: { script: './app.js' } } } }); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-express-server'); grunt.loadNpmTasks('grunt-contrib-watch'); // Default task(s). grunt.registerTask('server', [ 'express:dev', 'watch' ]); });