Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt watch not working

I tried to run the watch task by grunt in node.js but it doesn't work for me (this is what I got):

$ grunt watch
warning: Maximum call stack size exceeded Use --force to continue.

This is the part of the watch task in the Gruntfile.js:

watch: {
  less: {
    files: 'src/less/*.less',
    tasks: ['clean', 'recess', 'copy']
  },
  js: {
    files: 'src/js/*.js',
    tasks: ['clean', 'jshint', 'concat', 'uglify', 'copy']
  },
  theme: {
    files: 'src/theme/**',
    tasks: ['clean', 'recess', 'copy']
  },
  images: {
    files: 'src/images/*',
    tasks: ['clean', 'recess', 'copy']
  }
}

grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('watch', ['watch']);
like image 278
elkebirmed Avatar asked Oct 05 '13 11:10

elkebirmed


2 Answers

u_mulder is correct; simply remove the unnecessary grunt.registerTask('watch', ['watch']) line from your code and you should be good to go.

Edit: This happens because you are registering a new task that calls itself. Adding a line like grunt.registerTask('watch', ['watch']); doesn't make sense because it is already defined for you. If this wasn't the case you would have to call grunt.registerTask for each and every task in your Gruntfile config.

In some cases it might make sense to alias the task with a different name. It would be called with the exact same configuration that you have specified, but aliasing it could save typing. For instance I like to register my available tasks plugin with the 'tasks' alias, so instead of typing grunt availabletasks I can type grunt tasks and that saves me some typing. In this instance you could do something like:

grunt.registerTask('w', ['watch']);

And you can then use grunt w as a shortcut for grunt watch.

like image 199
Ben Avatar answered Nov 05 '22 09:11

Ben


Actually, deleting grunt.registerTask('watch', ['watch']) will sort you out. But let me help you to understand what's happening under the hood.

With grunt.registerTask('watch', ['watch']), watch is calling itself, which generates an infinite loop. When you delete it, it still works, cause watch is the default task of the package, which I guess is called at the very beggining of your file with grunt.loadNpmTasks('grunt-contrib-watch');. You can go further on doc here

However, it would be really handy to get your customization of the watch task to work as you want it to do. For this to happen, it would be probably better to do something like grunt.registerTask('watchfiles', ['watch']). With this you avoid the infinite loop and make your customization work.

And you would run the task like this $ grunt watchfiles and it would perform well.

Note that all the paths must be the right ones, otherwise, if a task has a wrong path specified, it will just not run.

like image 44
2682562 Avatar answered Nov 05 '22 07:11

2682562