Grunt watch not working

12,115

Solution 1

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.

Solution 2

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.

Share:
12,115
elkebirmed
Author by

elkebirmed

I'm a Full-stack web developer and a Graphic designer. I'm currently focused on: - Laravel framework - Wordpress - Web design - Node.js - Vue.js - Graphic design

Updated on July 21, 2022

Comments

  • elkebirmed
    elkebirmed almost 2 years

    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']);
    
  • 2682562
    2682562 about 10 years
    Ben, please reproduce the steps and then we can have a chat. The question asked here is not theorical, is something that has been blocking that developer on performing his grunt task. so as practical as it is, should be addressed 'practically' Your comment doesn't help on giving clarity to the issue. and if you reproduce the error you will get the endless loop as well. Then even if I'm wrong, it wouldn't really matter cause you will be giving more clarity over this 'darkness' to us all. Thx!