duojs / duo

A next-generation package manager for the front-end
3.42k stars 118 forks source link

requiring files is not idempotent if there is an error #421

Closed termhare closed 9 years ago

termhare commented 9 years ago

Say you have a file example.js that throws an error:

throw new Error('oops, something went wrong');

That situation can happen if a module not isn't found, for example. There's many other ways it can happen.

Then you require it. The first time, it will throw an error, but every time after that it will return a blank object {}.

var example = require('./example');
// Error: oops, something went wrong
var example = require('./example');
// {}
var example = require('./example');
// {}
// ..

It seems it should throw an error every time this happens. Otherwise things can get tricky. For example, we had something like this:

var example;
try {
  // for client
  example = require('segmentio/example');
} catch (e) {
  // for server
  example = require('example');
}

In this case, it threw the error in try block, and so went to the catch block. But require('example') also aliases to that same thing in the browser, so in the catch block it returned {}.

So basically, the error isn't caught until user interaction time, where maybe the user click and you call example.foo(), but foo is undefined.

If it always throws an error, it could make it easier for others.