1.0.0 • Published 8 years ago

require-or v1.0.0

Weekly downloads
615
License
ISC
Repository
github
Last release
8 years ago

require or ...

Converts:

var someModule;
try {
    someModule = require('module-that-might-fail');
} catch (e) {
    console.log('AARGH! Could not find someModule!');
}

... into:

var requireOr = require('require-or');
var someModule = requireOr('module-that-might-fail', function () {
    console.log('AARGH! Could not find someModule!');
});

The callback will be executed synchroniously and a return value will be returned from requireOr as well, allowing:

var someModule = requireOr('some-module', function () {
    return require('some-fallback-module');
});

The callback is not mandatory, so if you just want to squelch the error message, you don't need to provide it:

var maybeSomeModule = requireOr('some-module');

Relative paths works as well:

var maybeSomeModule = requireOr('../some-module', function () {
    // ...
});

Why?

There is multiple other modules trying to solve the same problem.

Both solve the problem of squelching errors, but it does not give you any improvements over vanilla require for the cases where you want to handle the fall back case.

License

This module is made public under the ISC License.

See the LICENSE file for additional details.