Skip to content

loader.js cannot handle *.cjs files #5220

Closed

Description

@TotallyInformation

Current Behavior

Reference: https://discourse.nodered.org/t/improvement-to-debugging-early-errors-when-loading-nodes/98197

I've found some small bugs in loader.js.

I've tested both of the proposed fixes.

loadPluginConfig

This function tries to set both plugin.file and plugin.template. At least one of these is needed for the call to loadNodeLocales(plugin). This can fail if the plugin js is in a file *.cjs rather than *.js even though *.cjs is a perfectly valid filename.

At the least, I believe loadPluginConfig should throw a meaningful error if neither file nor template properties exist.

Ideally, the *.js file check should be expanded to look for *.cjs if the first check fails. Possibly also look for *.mjs if needed to allow for future use of modules.

Example fix:

    var jsFile = file.replace(/\.[^.]+$/,".js");
    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;
    }
    if (!plugin.file && !plugin.template) {
        throw new Error(`Plugin file not found: "${file}"`);
    }

    await loadNodeLocales(plugin)

loadNodeConfig

This is a little simpler but fails for a similar but not quite identical issue. In this case, the file property is OK but the template property fails because, again, the replace assumes a source filename in the pattern *.js. This is actually an easier fix because we can easily extend the regex.

Old regex: /\.js$/i
New regex: /\.(js|cjs)$/i

Expected Behavior

The loader should be able to load both nodes and plugins having a file name *.cjs (and possibly also *.mjs).

Steps To Reproduce

Attempt to load a node or runtime plugin using cjs as the file extension.

Example flow

N/A

Environment

  • Platform/OS: Windows 11
  • Browser: N/A
0|Node-RED | 18 Jul 11:26:57 - [info] Node-RED version: v4.1.0-beta.2
0|Node-RED | 18 Jul 11:26:57 - [info] Node.js  version: v22.15.0
0|Node-RED | 18 Jul 11:26:57 - [info] Windows_NT 10.0.26100 x64 LE

Activity

  1. knolleary commented on Jul 21, 2025

    @knolleary
    Member

    @TotallyInformation your original forum post had a proposal around improving the logging - but you don't mention that here. I assume that is still relevant?

  2. knolleary commented on Jul 21, 2025

    @knolleary
    Member

    Fixed via #5221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions