Skip to content

Instantly share code, notes, and snippets.

@preslavrachev
Created March 18, 2015 07:39
Show Gist options
  • Save preslavrachev/7c22fc238bcec5ecab0e to your computer and use it in GitHub Desktop.
Save preslavrachev/7c22fc238bcec5ecab0e to your computer and use it in GitHub Desktop.
How to make nodemon and browserSync work together in a Gulp setup. From http://denbuzze.com/post/5-lessons-learned-using-gulpjs/
gulp.task('nodemon', function(cb) {
var nodemon = require('gulp-nodemon');
// We use this `called` variable to make sure the callback is only executed once
var called = false;
return nodemon({
script: 'server.js',
watch: ['server.js', 'server/**/*.*']
})
.on('start', function onStart() {
if (!called) {
cb();
}
called = true;
})
.on('restart', function onRestart() {
// Also reload the browsers after a slight delay
setTimeout(function reload() {
browserSync.reload({
stream: false
});
}, 500);
});
});
// Make sure `nodemon` is started before running `browser-sync`.
gulp.task('browser-sync', ['index', 'nodemon'], function() {
var port = process.env.PORT || 5000;
browserSync.init({
// All of the following files will be watched
files: ['public/**/*.*'],
// Tells BrowserSync on where the express app is running
proxy: 'http://localhost:' + port,
// This port should be different from the express app port
port: 4000,
// Which browser should we launch?
browser: ['google chrome']
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment