最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get notified of errors during when piping to gulp browserify? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Jan 20, 2015 at 17:38 mikemaccanamikemaccana 124k110 gold badges430 silver badges532 bronze badges 2
  • 2 Do you use browserify util or gulp-browerify plugin? In my browserify task i use browserify util and it inform me when js has errors. – alexmac Commented Jan 21, 2015 at 15:46
  • @Alexander Actually I think you've just answered the question. From my gulpfile: var browserify = require('gulp-browserify') which is no longer maintained - see npmjs./package/gulp-browserify. The proper thing looks to be to use another package. Do you want to post that as an answer? – mikemaccana Commented Jan 21, 2015 at 16:09
Add a ment  | 

3 Answers 3

Reset to default 6

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.

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.

I use gulp-plumber for all my error catching:

gulp.task('browserify', function(){
    return browserify('./assets/js/src/app.js')
            .pipe(plugins.plumber())
            .pipe(gulp.dest('./assets/js'))
            .pipe(plugins.notify({message: "browserify plete"}))
});

https://www.npmjs./package/gulp-plumber

You might also have to use gulp-streamify. I don't think browserify supports streams out of the box. It's been some time since I tried browserify with gulp so I'm not sure my code snippet will work out of the box, sorry for that.

https://github./nfroidure/gulp-streamify

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>