30

I'm new to Gulp and I wanted to make use of its automatic scss compiling and browser sync. But I can't get it to work.

I stripped everything down to leave only the contents of the example on the Browsersync website:

http://www.browsersync.io/docs/gulp/#gulp-sass-css

var gulp        = require('gulp');
var browserSync = require('browser-sync').create();
var sass        = require('gulp-sass');

// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {

    browserSync.init({
        server: "./app"
    });

    gulp.watch("app/scss/*.scss", ['sass']);
    gulp.watch("app/*.html").on('change', browserSync.reload);
});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream());
});

gulp.task('default', ['serve']);

I can call gulp serve. The site is showing and I get a message from Browsersync. When I modify the HTML, the page is reloaded. When however I modify the scss, I can see this:

[BS] 1 file changed (test.css)
[15:59:13] Finished 'sass' after 18 ms

but I have to reload manually. What am I missing?

raichu
  • 688
  • 2
  • 8
  • 15

8 Answers8

62

I also faced a similar problem when I was new to browser-sync usage, the command-line was saying "reloading browsers" but the browser was not refreshed at all, the problem was I had not included body tag in my HTML page where the browser-sync can inject script for its functionality, make sure your HTML page has body tag.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
SU15
  • 873
  • 2
  • 11
  • 15
  • 4
    Thank you for saving me days of frustration. Frankly this has nothing to do with Gulp so probably deserves its own question/answer because I know I'm not the only one who first tested this in a lazy way with a simple tag-free `index.html` file and wondered...hey why isn't this working? – eric Oct 09 '17 at 03:58
  • 1
    Precious, precious knowledge, sir. – slacktracer Jun 04 '18 at 04:10
  • 3
    I was having exactly the same problem! Left out body tags for a quick test and then could not figure out why browser sync had stopped working... Much appreciated!!! – Shan Dou Jan 04 '19 at 17:04
  • 2
    This simple concept is something I must have missed in reading about BrowserSync. Thanks for posting, just ended 2 hours of frustration. – Rob Keown Apr 20 '19 at 15:58
  • 2
    If you really don't want to use a `

    ` tag in my HTML, you can use the [`snippetOptions`](https://browsersync.io/docs/options/#option-snippetOptions) option to inject the BrowserSync script in another location of your HTML files.

    – Cobertos Oct 11 '19 at 21:49
  • This is really helpful. – Derick Sep 06 '21 at 12:51
  • i lost lots of hours with that, and @SU15 saved my whole day. amazing job. – ratyacatnug Jul 02 '23 at 17:16
16

You can just inject the changes instead of having to force a full browser refresh on SASS compile if you like.

browserSync.init({
    injectChanges: true,
    server: "./app"
});

gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream({match: '**/*.css'}));
});
Dan Gamble
  • 3,965
  • 1
  • 24
  • 44
12

This is because you're calling browserSync.reload on the html watch and not on the scss watch.

Try this:

gulp.watch("app/scss/*.scss", ['sass']).on('change', browserSync.reload);
gulp.watch("app/*.html").on('change', browserSync.reload);
Felipe Skinner
  • 16,246
  • 2
  • 25
  • 30
6

This is what I use and it work's fine in sass or any other files

gulp.task('browser-sync', function () {
   var files = [
      '*.html',
      'css/**/*.css',
      'js/**/*.js',
      'sass/**/*.scss'
   ];

   browserSync.init(files, {
      server: {
         baseDir: './'
      }
   });
});
SimplifyJS
  • 518
  • 1
  • 6
  • 12
  • I think this answer is better; however, in my case (perhaps because I'm using a proxy), I am not able to use the `'./**/*'` glob pattern. – bozdoz Apr 24 '16 at 05:55
  • 1
    ^ I realize now that my problem was that I was watching the node_modules directory – bozdoz Apr 24 '16 at 06:12
  • this is the simplest possible answer and it still works out of the box with the latest stable browser-sync. – Mario Jun 26 '17 at 20:53
2

I include this on my html, right below the body tag. It works.

<script type='text/javascript' id="__bs_script__">//<![CDATA[
document.write("<script async src='http://HOST:3000/browser-sync/browser-sync-client.2.11.1.js'><\/script>".replace("HOST", location.hostname));//]]>
</script>
  • Works like a charm! Just don't forget to create a check that looks at some env variable if you're on your local development branch :) – 0xe1λ7r Jan 28 '20 at 14:32
2

Ran into this same problem trying to reload php and js files and stream css files. I was able to use stream only by using a pipe method, which makes sense. Anyway, here's what worked for me:

gulp.watch(['./**/*.css']).on('change', function (e) {
    return gulp.src( e.path )
        .pipe( browserSync.stream() );
});

But, I actually prefer @pixie's answer modified:

gulp.task('default', function() {
    var files = [
            './**/*'
        ];
    browserSync.init({
        files : files,
        proxy : 'localhost',
        watchOptions : {
            ignored : 'node_modules/*',
            ignoreInitial : true
        }
    });
});
bozdoz
  • 12,550
  • 7
  • 67
  • 96
2

I also had the same issue. It worked when I called the reload method as a separate task.

gulp.task('browserSync', function() {
  browserSync.init(null, {
    server: {
      baseDir: './'
    },
  });
})

gulp.task('reload', function(){
    browserSync.reload()
})

gulp.task('watch', ['sass', 'css', 'browserSync'], function(){
   gulp.watch('*.html', ['reload']);
})
Lasithds
  • 2,161
  • 25
  • 39
  • I was going to type this answer, but since its already here, I upvoted. I found the answer here as well: https://www.youtube.com/watch?v=4y8Iw85__Xk – Riza Khan Jul 12 '20 at 04:46
  • Throws an error: `AssertionError [ERR_ASSERTION]: Task function must be specified` – john Apr 08 '21 at 18:55
1

Sometimes when using the CLI you don't have the script inserted in your HTML main files so you should manually add this or use gulp.

    <!-- START: BrowserSync Reloading -->
    <script type='text/javascript' id="__bs_script__">
        //<![CDATA[
        document.write("<script async src='/browser-sync/browser-sync-client.js'><\/script>".replace("HOST", location.hostname));
        //]]>
    </script>
    <!-- END: BrowserSync Reloading -->
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56