force bundle() to throw error when module not found

I'm using gulp with browserify. I have a bundle() function which is like this:

    function bundle(script) {

      return browserify( get_browserify_config(script) )
        .bundle()
        .on('error', function(err){
            notifyError('error while bundling ' + script );
            console.log('-- error while bundling ' + script + ' --');
            errorLog(err);
        })
        .pipe(source(script))
        .on('error', errorLog)
        .pipe(buffer())
        .pipe(gulpIf(!isProductionBuild, sourcemaps.init()))
        .pipe(uglify())
        .on('error', errorLog)
        .pipe(gulpIf(!isProductionBuild, sourcemaps.write('./')))
        .pipe(gulp.dest(PATH_TO_SERVE_SCRIPTS))
        .on('error', errorLog)
        .pipe(gulp.dest(PATH_TO_TARGET_SCRIPTS))
        .on('error', errorLog)
        .on('end', function() {
            jsLog('finished bundle of ' + script);
        });
}

It works fine when I pass scripts with valid require dependencies to it. However, if I have bad dependencies, it doesn't throw any error.

For instance, if I have a file 'app.js' which requires a non-existing module like:

require('./xyzrandomnotexistingmodulename');

I don't get any error during its bundle. I just get an app.js bundled file on the PATH_TO_TARGET_SCRIPTS which just complains about module not being found:

!function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var a="function"==typeof require&&require;if(!f&&a)return a(i,!0);if(u)return u(i,!0);var c=new Error("Cannot find module '"+i+"'");throw c.code="MODULE_NOT_FOUND",c}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n?n:r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({},{},[]);

I would prefer to have browserify to stop bundling and throw an error rather than generating a not working bundle.

Is there a way to configure browserify to throw an Error or emit an error event instead of silently continue bundling when it doesn't find a module?

链接地址: http://www.djcxy.com/p/32916.html

上一篇: 配置watchify和browserify

下一篇: 强制bundle()在找不到模块时抛出错误