Given the following piece of code from my gulpfile.js, everytime I save or change a file, the task runs twice instead of one single time, why is that? I just want it to run one time.
var gulp = require('gulp');
gulp.task('default', function() {
gulp.watch('server/**/*.js', function(){
console.log('This runs twice everytime I change/save a javascript file located at server/**/*.js');
});
});
I have also experienced the same with grunt and the plugin called grunt-contrib-watch.
I write here my experience, maybe helps someone. This cost me 1 week to detect. I made every possible debug. Removed files, reinstalled clean node packages. Removed Sublime plugins. Reported as a bug to Sublime github and plugins github pages.
My solution Close dropbox if open. I use Dropbox for my project and work there. When Dropbox is open, tasks run twice because Dropbox detects file change and does something. Specially Typescript files, i don't know why.
I was seeing a similar issue, but it was caused by having the page open in multiple tabs/windows.
I tried debounce and awaitWriteFinish. It didn't work. This did:
Seems like the answer to this question is a feature of the editor that was used, Coda 2. Based on some of the comments here and testing with multiple editors, it seems like Coda 2 saves a temporary file or similar and that causes the gulp watch function to be run twice.
I have not found a viable solution to this when using Coda 2, ended up with switching to Sublime Text 3.
Aside from editor specific solutions, I wrote a little gulp plugin that solves the problem. You can use
gulp-debounce
like so:npm install --save-dev gulp-debounce
I add the same problem in Espresso and you can "fix" it in Gulp by using the debounceDelay option like this :
That did the trick for me. But it's a pain to add it to every gulp.watch I don't know if we can put this option globaly...