require-maybe
v1.0.0
Published
Optionally require a module, maybe fallback to others.
Downloads
2
Readme
require-maybe
Optionally require a module, optionally falling back to others.
- returns
undefined
if the optional module(s) can't be found - without throwing an exception
- can attempt to load alternative modules
- works with all paths (unlike similar modules)
This can be useful for libraries that want to use certain features when the optional modules are installed, without pulling in additional dependencies.
Table of Contents
Install
npm:
npm install require-maybe
yarn:
yarn add require-maybe
Usage
const requireMaybe = require('require-maybe');
function noop(x) {
return x
}
// use `rainbow` if it’s installed, or normal text if it isn't
const rainbow = requireMaybe('chalk-rainbow') || noop
console.log(rainbow('hello world!'))
// -> rainbowified or plain "hello world!"
// Use either implementation of `camelcase`, or none
const camelcase = requireMaybe(['lodash.camelcase', 'camelcase']) || noop
console.log(camelcase('hello world!'))
// -> "helloWorld!" or "hello world!"
Contributors
| Name | Website | | ----------------- | --------------------------------- | | Andreas Pizsa | https://github.com/AndreasPizsa |