require-implementation

Package that helps you to be okay if some dependency is missing.


Keywords
clean, code, optional, dependency, dependencies
License
ISC
Install
npm install require-implementation@1.0.1

Documentation

Build Status Coverage Status js-standard-style

require-implementation

Package that helps you to be okay if some dependency is missing.

Usage

const impl = require('require-implementation')

try {
  // Let the user know what action you would like to take.
  const math = impl('Trying to do some math.')
    // ... then require what ever package could
    //     installed immediatly.
    .require('math')
} catch (e) {
  // You can filter other errors by checking for the code
  e.code === 'EPACKAGEMISSING'

  // In recommended packages 
  e.recommendedPackages == ['math']

  e.message === 'EPACKAGEMISSING: Trying to do some math.\n'+
                'This error can be easily fixed by running the following command:\n'+
                '$ npm install --save math'
}

In case there are multiple options:

const impl = require('require-implementation')
try {
  // requireFirst lets people know that each of the 
  const mathSet = impl('Trying to do some math.').requireFirst(['math', 'mathjs'])

  // You can find out which was ldaded by taking the name
  mathSet.name === 'math' || mathSet.name === 'mathjs'

  // The required package can be found in .pkg
  const math = mathSet.pkg
} catch (e) {
  // Same error as above
}

License

ISC