require-implementation
v1.0.1
Published
Package that helps you to be okay if some dependency is missing.
Downloads
46
Maintainers
Readme
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