node-require-fallback
Advanced tools
Comparing version 0.1.2 to 1.0.0
20
index.js
@@ -1,4 +0,4 @@ | ||
function exists(nodeModule) { | ||
function exists(m) { | ||
try { | ||
require.resolve(nodeModule) | ||
require.resolve(m) | ||
} catch (e) { | ||
@@ -11,15 +11,9 @@ return false; | ||
function requireIfExists(nodeModule, fallbackModule1, fallbackModule2) { | ||
if (exists(nodeModule)) { | ||
return require(nodeModule) | ||
function requireIfExists(...modules) { | ||
for (m of modules) { | ||
if (exists(m)) { | ||
return require(m); | ||
} | ||
} | ||
if (exists(fallbackModule1)) { | ||
return require(fallbackModule1); | ||
} | ||
if (exists(fallbackModule2)) { | ||
return require(fallbackModule2); | ||
} | ||
return null; | ||
@@ -26,0 +20,0 @@ } |
{ | ||
"name": "node-require-fallback", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Require another package in Node.js if your first choice doesn't exist.", | ||
@@ -9,2 +9,5 @@ "repository": { | ||
}, | ||
"engines": { | ||
"node": ">= 6" | ||
}, | ||
"main": "index.js", | ||
@@ -11,0 +14,0 @@ "keywords": [ |
@@ -6,3 +6,4 @@ # node-require-fallback | ||
Useful for development, where you want to use a local copy of your package, | ||
but it shouldn't break the application for your team, which is depending an npm package. | ||
but it shouldn't break the application for your team, which is depending on a | ||
npm package. | ||
@@ -15,2 +16,4 @@ # Installation | ||
> Node.js >= 6 is required for this package. | ||
# Example Usage | ||
@@ -21,6 +24,7 @@ | ||
var myModule = requireIfExists('module1', 'module2'); | ||
var myModule = requireIfExists('./module1', 'module2', 'module3'); | ||
``` | ||
`myModule` will now hold `module1` if it exists or `module2` otherwise. | ||
`myModule` will be `null`, if `module2` wasn't available as well. | ||
You can pass as many modules to `requireIfExists` as you want. | ||
The first one that can be found will be loaded, otherwise `null` | ||
will be returned. |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
28
2
2791
17