require-maybe

1.0.0 • Public • Published

require-maybe

build status code coverage code style styled with prettier made with lass license

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

License

MIT © Andreas Pizsa

Readme

Keywords

Package Sidebar

Install

npm i require-maybe

Weekly Downloads

4

Version

1.0.0

License

MIT

Last publish

Collaborators

  • andreaspizsa