1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fallback to 'require' if on node < 12.17

This commit is contained in:
Nick O'Leary 2021-07-15 15:37:12 +01:00
parent 31c5d6e1c1
commit 66a704af55
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 19 additions and 1 deletions

View File

@ -494,7 +494,7 @@ module.exports = function(RED) {
node.error(err);
}
}).catch(err => {
throw new Error(RED._("function.error.externalModuleLoadError"));
node.error(RED._("function.error.externalModuleLoadError"));
});
}
RED.nodes.registerType("function",FunctionNode, {

View File

@ -26,6 +26,12 @@ let installEnabled = true;
let installAllowList = ['*'];
let installDenyList = [];
let IMPORT_SUPPORTED = true;
const nodeVersionParts = process.versions.node.split(".").map(v => parseInt(v));
if (nodeVersionParts[0] < 12 || (nodeVersionParts[0] === 12 && nodeVersionParts[1] < 17)) {
IMPORT_SUPPORTED = false;
}
function getInstallDir() {
return path.resolve(settings.userDir || process.env.NODE_RED_HOME || ".");
}
@ -102,6 +108,18 @@ function requireModule(module) {
return require(moduleDir);
}
function importModule(module) {
if (!IMPORT_SUPPORTED) {
// On Node < 12.17 - fall back to try a require
return new Promise((resolve, reject) => {
try {
const mod = requireModule(module);
resolve(mod);
} catch(err) {
reject(err);
}
});
}
if (!registryUtil.checkModuleAllowed( module, null,installAllowList,installDenyList)) {
const e = new Error("Module not allowed");
e.code = "module_not_allowed";