1.0.0 • Published 8 years ago
require-maybe v1.0.0
require-maybe
Optionally require a module, optionally falling back to others.
- returns
undefinedif 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-maybeyarn:
yarn add require-maybeUsage
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 |
License
1.0.0
8 years ago