Skip to content

Broken plugin's error handler crashes with ReferenceError (node is not defined) - loadPlugin catch block references node.file #6008

Closed

Description

@rahul05ranjan

Current Behavior

When a plugin's runtime JS file throws while being loaded (e.g. a syntax error, or a throw at module top level), the catch block in loadPlugin() crashes with ReferenceError: node is not defined before it can record a useful error.

loadPlugin() takes a parameter called plugin, but the stack-parsing line in its catch block was copied from loadNodeSet() and still dereferences node.file:

packages/node_modules/@node-red/registry/lib/loader.js (function starts at line 438):

    } catch(err) {
        console.log(err);
        plugin.err = err;
        var stack = err.stack;
        if (stack) {
            var i = stack.indexOf(plugin.file);
            if (i > -1) {
                var excerpt = stack.substring(i+node.file.length+1,i+plugin.file.length+20);   // <-- line 471: `node` is not defined here
                var m = /^(\d+):(\d+)/.exec(excerpt);
                if (m) {
                    plugin.err = err+" (line:"+m[1]+")";
                }
            }
        }
        return plugin;
    }

Because loadPlugin is async, the ReferenceError turns into a rejected promise, and the only reason startup survives is that loadNodeSetList() swallows it:

promises.push(loadPlugin(node).catch(err => {}));   // line 487

Net effect for a plugin whose JS file fails to load:

  • the intended plugin.err = err+" (line:N)" enrichment never happens (that code is unreachable),
  • plugin.err stays a raw Error object, which serialises as an empty object {} in the Admin API plugin/module listings, so the editor shows an error entry with no usable message,
  • the same file failing as a node set gets the proper handling, because loadNodeSet() (line 379) has the matching code where node actually is the parameter name.

Expected Behavior

Same as for node sets: plugin.err should become something like SyntaxError: Unexpected identifier 'is' (line:2), and the plugin should be reported as failed with that message in the plugin listing.

Steps To Reproduce

  1. Create a small module in ~/.node-red/nodes that declares a plugin:
{
    "name": "test-plugin",
    "version": "1.0.0",
    "node-red": { "plugins": { "my-plugin": "lib/my-plugin.js" } }
}
  1. Put a file with a syntax error in lib/my-plugin.js:
module.exports = function(RED) {
    this is not valid javascript!!!
};
  1. Start Node-RED and open the Palette Manager / GET /nodes — the plugin shows an error, but the detail is an empty {} instead of the actual error with line number.

Minimal demo using the real function extracted verbatim from loader.js:

// fixture: my-plugin.js  ->  "this is not valid javascript!!!" (SyntaxError)
const { loadPlugin } = require("./_loadplugin_extracted.js"); // verbatim loadPlugin from loader.js

const plugin = { type: "plugin", id: "test-plugin/my-plugin", module: "test-plugin",
                 name: "my-plugin", file: "/abs/path/my-plugin.js", enabled: true };

try {
    await loadPlugin(plugin);
} catch (e) {
    console.log(e);                 // ReferenceError: node is not defined
    console.log(JSON.stringify(plugin.err)); // "{}"
}

Output on my machine:

SyntaxError: Unexpected identifier 'is'        <- console.log(err) in the catch block
[A] loadPlugin REJECTED with: ReferenceError: node is not defined
    plugin.err left as raw Error -> SyntaxError -> JSON.stringify(plugin.err) = {}

Suggested Fix

Use the right variable in loadPlugin (and ideally widen the substring bounds the same way loadNodeSet does):

- var excerpt = stack.substring(i+node.file.length+1,i+plugin.file.length+20);
+ var excerpt = stack.substring(i+plugin.file.length+1,i+plugin.file.length+20);

Possibly also worth replacing the silent loadPlugin(node).catch(err => {}) in loadNodeSetList with a log, though the missing-file side of silent plugin failures is already tracked in #5840 (this is the separate "file exists but throws" path).

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions