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.
The problem is occurring because your editor, in this case Coda 2, is modifying the file twice on save. The same problem occurs in vim because of how vim creates buffer backups on save.
The solution to the problem in vim is to add
set nowritebackup
to your ~/.vimrc
file. This changes the default save protocol to only make one edit to the original file.
In other words, the default save protocol is as follows:
And adding set nowritebackup
simply replaces the original file on save. This protocol exists to reduce risk of data loss in the event of an I/O error on save.
I add the same problem in Espresso and you can "fix" it in Gulp by using the debounceDelay option like this :
gulp.watch('/**/*.less', {debounceDelay: 2000}, ['less']);
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...
This worked for me
.pipe(watch('/**/*.less', { awaitWriteFinish: true }))
https://github.com/paulmillr/chokidar#api
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