What's this then?
A micro module that helps you to require (versions of) modules
that might not be there. Useful to test for the availability of
optional and peer dependencies before working with them.
The reason this module exists is to try a few non-standard
things with the npm registry (deprecating, publishing betas,
...).
Feel free to use it, though - semver-try-require
works
as advertised and is thoroughly tested.
Example
So you made the typescript compiler (v2) an optional dependency.
But you just want to keep running if it ain't there.
Do this:
const tryRequire = require('semver-try-require');
const typescript = tryRequire('typescript', '>=2');
const lProgram = 'const cube = x => x*x*x; console.log(cube(42))';
if (typescript !== false) {
console.log(
typescript.transpileModule(lProgram, {}).outputText
);
} else {
console.log(
lProgram
);
}
Signature
pModulename
The name of the module to resolve.
pSemVer
A semantic version (range). Optional.
return value
The (resolved) module identified by pModuleName if:
- it is available, and
- it satisfies the semantic version range specified by pSemVer
returns false in all other cases
License
MIT
Badge & flare section

Made with :metal: in Holland