From 1749ef7ac0534b9b745311b015df33549f1f0317 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 21 Jul 2025 16:19:11 +0100 Subject: [PATCH] Support cjs files --- .../@node-red/registry/lib/loader.js | 22 ++++++++++++++----- .../@node-red/registry/lib/localfilesystem.js | 6 ++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/node_modules/@node-red/registry/lib/loader.js b/packages/node_modules/@node-red/registry/lib/loader.js index eb27d9411..4e299ca77 100644 --- a/packages/node_modules/@node-red/registry/lib/loader.js +++ b/packages/node_modules/@node-red/registry/lib/loader.js @@ -54,11 +54,12 @@ function loadModuleTypeFiles(module, type) { for (let thingName in things) { /* istanbul ignore else */ if (things.hasOwnProperty(thingName)) { + const thing = things[thingName] if (module.name != "node-red" && first) { // Check the module directory exists first = false; let moduleFn = module.path - const fn = things[thingName].file + const fn = thing.file const parts = splitPath(fn) const nmi = parts.indexOf('node_modules') if(nmi > -1) { @@ -78,9 +79,9 @@ function loadModuleTypeFiles(module, type) { try { let promise; if (type === "nodes") { - promise = loadNodeConfig(things[thingName]); + promise = loadNodeConfig(thing); } else if (type === "plugins") { - promise = loadPluginConfig(things[thingName]); + promise = loadPluginConfig(thing); } promises.push( promise.then( @@ -91,10 +92,14 @@ function loadModuleTypeFiles(module, type) { return nodeSet; } })() - ).catch(err => {console.log(err)}) + ).catch(err => { + // This shouldn't fail - but if it does, we don't want to crash the loader + // But we also need to log it to have some chance of figuring out what went wrong + console.log(thing.name, err) + }) ); } catch(err) { - console.log(err) + console.log(thing.name, err) } } } @@ -265,7 +270,7 @@ async function loadNodeConfig(fileInfo) { module: module, name: name, file: file, - template: file.replace(/\.js$/,".html"), + template: file.replace(/\.c?js$/,".html"), enabled: isEnabled, loaded:false, version: version, @@ -324,6 +329,11 @@ async function loadPluginConfig(fileInfo) { var htmlFile = file.replace(/\.[^.]+$/,".html"); if (fs.existsSync(jsFile)) { plugin.file = jsFile; + } else { + jsFile = file.replace(/\.[^.]+$/,".cjs") + if (fs.existsSync(jsFile)) { + plugin.file = jsFile; + } } if (fs.existsSync(htmlFile)) { plugin.template = htmlFile; diff --git a/packages/node_modules/@node-red/registry/lib/localfilesystem.js b/packages/node_modules/@node-red/registry/lib/localfilesystem.js index 0c231552f..d11b6c579 100644 --- a/packages/node_modules/@node-red/registry/lib/localfilesystem.js +++ b/packages/node_modules/@node-red/registry/lib/localfilesystem.js @@ -72,11 +72,11 @@ function getLocalFile(file) { return null; } try { - fs.statSync(file.replace(/\.js$/,".html")); + fs.statSync(file.replace(/\.c?js$/,".html")); return { file: file, module: "node-red", - name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""), + name: path.basename(file).replace(/^\d+-/,"").replace(/\.c?js$/,""), version: settings.version }; } catch(err) { @@ -114,7 +114,7 @@ function getLocalNodeFiles(dir, skipValidNodeRedModules) { files.forEach(function(fn) { var stats = fs.statSync(path.join(dir,fn)); if (stats.isFile()) { - if (/\.js$/.test(fn)) { + if (/\.c?js$/.test(fn)) { var info = getLocalFile(path.join(dir,fn)); if (info) { result.push(info);