I'm just getting started with Grunt and would like to run grunt-contrib-watch [GitHub page] to lint my JavaScript every time a file is modified (with grunt-contrib-jshint [GitHub page]) and run grunt-nodemon [GitHub page] too, concurrently using grunt-concurrent [GitHub page].
As I understand (which I evidently don't) my Gruntfile should:
concurrent by defaultconcurrent runs watch
watch runs jshint every time a file is modifiedGruntfile.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'
    ]);
};
N.B. I've not added grunt-nodemon into the mix yet.
It looks like concurrent is running watch but when I modify a file it appears jshint isn't running. I certainly don't get any output in the Terminal (I thought logConcurrentOutput: true does this).
Here is the output I get in the Terminal:
Running "concurrent:dev" (concurrent) task
Running "watch" task
Waiting...    
Done, without errors.
I would also like to run jshint when I first run the default task (as well as when I modify files).
Can anyone shed some light on where I am going wrong?
Thanks!
Concurrently is an npm package that allows you to run multiple commands concurrently.
Meaning of concurrently in Englishat the same time: Her two dramas are being shown concurrently by rival television stations. She will learn today whether her two life sentences will run concurrently or consecutively. Mr.
Concurrently is a Node package that allows you to run multiple scripts at the same time in Node. js. It's especially useful if you want to run your app's front-end and back-end from a single NPM command. Concurrently also has a Node API to start concurrent processes programmatically from within a script file.
The watch task exits if there are no files found to 'watch'; as per this issue.
To easily tell watch to watch the same files as the jshint task I used Grunt's templating engine to reference the same Array of files as the jshint task.
I then added jshint to the list of concurrent tasks to run so it would be ran initially and as I modify files (with watch).
Here is my working 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'
    ]);
};
If you are running an Express server then you can use grunt-express-server. The documentation has a good guide on using this with JSHINT, and 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' ]);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With