A simple replacement for
require
with an optional fallback if the target module doesn't exist instead of throwing.
npm install --save require-me-maybe
const requireMeMaybe = require('require-me-maybe')
const file = process.env.EXAMPLE_CONFIG_FILE
require(file) // => this could throw
requireMeMaybe(file, {
// fallback defaults here
}) // this will never throw but rather return the fallback
Returns either the required module or the defaults. Defaults are returned if the desired module is undefined
or not found.
Note that any other errors encountered while requiring the target module will still be thrown, including syntax errors.
MIT © Travis Fischer