browserify / watchify

watch mode for browserify builds
Other
1.79k stars 203 forks source link

How to stop watchify/browserify exploding on error? #241

Closed jedrichards closed 9 years ago

jedrichards commented 9 years ago

Hi

My watchify+browserify script is exiting with a non-zero error code when something goes awry with the browserify transpilation. I'm happy with this during normal (non-watchified) browserfication, its appropriate that it bombs out, but during development I'd rather the watch stayed alive so I don't need to restart it each time.

An example of an error that causes it to explode might be mistyping the path to a module.

events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: Cannot find module 'x' from 'y'

How can I handle this error?

I'm not sure if the fact that I'm using babelify, and also calling my browserify script itself with babel-node is relevant. I suspect not ...

Here's my watchify+browserify script,

import fs from 'fs';
import browserify from 'browserify';
import babelify from 'babelify';
import envify from 'envify/custom';
import watchify from 'watchify';

const {WATCHIFY,NODE_ENV} = process.env;

var b = browserify({
  cache: {},
  packageCache: {},
  entries: ['./src/main.js']
});

function bundle () {
  b.bundle().pipe(fs.createWriteStream('./public/main.bundle.js'));
}

if (WATCHIFY) b = watchify(b);

b.transform(babelify.configure({
  stage: 0,
  optional: ['runtime']
}));

b.transform(envify({
  NODE_ENV: NODE_ENV
}));

b.on('update',bundle);
b.on('log',console.log);
b.on('error',console.error);

bundle();
zertosh commented 9 years ago

You need an error handler on the stream returned by bundle() – that's just how streams work. You should try https://github.com/zertosh/errorify

jedrichards commented 9 years ago

Ah yes, of course a stream error handler ... been so long I'd done any stream stuff that I'd forgot about that sort of thing. Thanks so much.