mindee
Version:
Mindee Client Library for Node.js
21 lines (20 loc) • 862 B
JavaScript
/**
* Helper to load optional peer dependencies.
* @param packageName The name of the npm package to load
* @param featureName A human-readable name of the feature requiring this package
*/
export async function loadOptionalDependency(packageName, featureName) {
try {
return await (new Function("specifier", "return import(specifier)")(packageName));
}
catch (error) {
if (error.code === "MODULE_NOT_FOUND" ||
error.code === "ERR_MODULE_NOT_FOUND" ||
error.message?.includes("Cannot find module") ||
error.message?.includes("Cannot find package")) {
throw new Error(`The feature '${featureName}' requires the optional dependency '${packageName}'. ` +
"Please install optional dependencies: `npm install --include=optional`");
}
throw error;
}
}