Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get notified of errors during when piping to gulp browserify?

I am using browserify so I can use npm modules in my front end code, and gulp to do my build tasks. This works fine:

var browserify = require('gulp-browserify');

gulp.task('js', ['clean'], function() {
    gulp
        .src('./public/js/src/index.js')
        .pipe(browserify({
            insertGlobals : true,
            debug : ! gulp.env.production
        }))
        .pipe(gulp.dest('./public/js/dist'))
});

However if there's a syntax error in my JS, I'd like to be notified of the error via an OS X notification. I've seen this similar question and modified my code to add an .on('error'...) after the .browserify():

// Browserify/bundle the JS.
gulp
    .src('./public/js/src/index.js')
    .pipe(browserify({
        insertGlobals : true,
        debug : ! gulp.env.production
    }).on('error', function(err){
        notify.onError({
            message: "Error: <%= error.message %>",
            title: "Failed running browserify"
        }
        this.emit('end');
    })
    .pipe(gulp.dest('./public/js/dist'))

However this doesn't notify when my JS is broken. Adding a console.log() inside on('error',...) doesn't log either. I suspect because that question doesn't involve using gulp piping.

How can I get notified of errors during when piping to gulp browserify?

like image 638
mikemaccana Avatar asked Jan 20 '15 17:01

mikemaccana


2 Answers

Use gulp-notify. Note also you should use the browserify module instead of the deprecated gulp-browerify module.

var browserify = require('browserify'),
    brfs = require('brfs'),
    watchify = require('watchify'),
    notify = require("gulp-notify");


var bundler = watchify(browserify({
    basedir: "./public/js/src"
}));


// Browserify our code
gulp.task('js', ['clean'], function() {
    // Browserify/bundle the JS.
    return bundler.bundle()
        // log errors if they happen
        .on('error', function(err) {
            return notify().write(err);
        })
        .pipe(source('index.js'))
        .pipe(gulp.dest('public/js/dist'));
});

Here example how to use browserify util in gulp task.

Here another example how to use browserify and show notification, when js has syntax errors.

like image 124
alexmac Avatar answered Oct 20 '22 00:10

alexmac


In my gulpfile, I just listen for uncaught exceptions and notify using node-notifier:

var Notifier = require('node-notifier');
notifier = new Notifier();

process.on('uncaughtException', function(err) {
  notifier.notify({title: 'error', message: 'gulp process has crashed'});

  setTimeout(function() {
    console.error(err);
    process.exit(1);
  }, 1000);
});

I found that a setTimeout delay was necessary for the notification to show up before exiting the process.

like image 38
Ben Avatar answered Oct 19 '22 23:10

Ben