Skip to content

Optionally require a module, maybe falling back to others.

License

Notifications You must be signed in to change notification settings

AndreasPizsa/require-maybe

Repository files navigation

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

About

Optionally require a module, maybe falling back to others.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published