require-implementation

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i require-implementation

Weekly Downloads

2

Version

1.0.1

License

ISC

Last publish

Collaborators

  • leichtgewicht