Skip to content

removeModule(): module.usedBy > 0 guard never fires (array compared with number) — removing a module another module depends on fully unregisters it #6010

Closed

Description

@rahul05ranjan

Current Behavior

If module B is a runtime dependency of an installed module A — i.e. moduleConfigs[B].usedBy contains "A", as recorded by loader.addModule() via registry.addModuleDependency(dep, moduleToLoad) (loader.js:527) — removing B from the palette manager (admin DELETE /nodes/B) fully unregisters B: its moduleConfigs entry is deleted, its node constructors/types are removed, and the npm uninstall that follows deletes it from disk. Module A is left broken: its dependency is gone, and flows using its node types fail with unknown types on the next deploy/restart.

Expected Behavior

registry.removeModule() has an explicit branch for exactly this case (registry.js:274):

if (module.usedBy && module.usedBy > 0) {
    // We are removing a module that is used by other modules... so whilst
    // this module should be removed from the editor palette, it needs to
    // stay in the runtime... for now.
    module.user = false;
    for (var i=0;i<nodes.length;i++) {
        infoList.push(filterNodeInfo(nodes[i]));
    }
}

B should stay loaded in the runtime (only hidden from the palette), and A keeps working.

That branch never runs, because moduleConfigs[x].usedBy is an array of module-name strings (registry.js:670-673):

function addModuleDependency(module,usedBy) {
    moduleConfigs[module].usedBy = moduleConfigs[module].usedBy || [];
    moduleConfigs[module].usedBy.push(usedBy);
}

and module.usedBy > 0 compares that array with a number: ["my-nodes"] > 0 is NaN > 0 → false, [] > 0 → 0 > 0 → false, undefined > 0 → false. The guard is false in every case, so the intended "keep in runtime" branch is dead code. The correct form is already used elsewhere in the same file (registry.js:495, getAllNodeConfigs):

if (!module.user && (module.usedBy && module.usedBy.length > 0)) {

Steps to Reproduce

  1. Palette manager: install module dep-node (user-installed, loaded and registered).
  2. Palette manager: install module my-nodes whose package.json declares dep-node as a dependency and uses it at runtime. Since dep-node is already registered at that point, loader.addModule() records dep-node.usedBy = ["my-nodes"] (loader.js:527).
  3. Palette manager: remove dep-node while my-nodes is still installed.

Actual: dep-node is fully unregistered (registry.js:528 → registry.removeModule) and then npm-uninstalled, breaking my-nodes. Expected per the comment: stays in the runtime, only user = false.

To verify this is not just a reading of the code, I extracted removeModule/removeNode/filterNodeInfo verbatim from current main and ran them against a registry state where dep-node.usedBy = ["my-nodes"] and my-nodes is installed:

== guard evaluation (registry.js:274) ==
module.usedBy = ["my-nodes"]
module.usedBy && module.usedBy > 0  -> false
fixed guard: usedBy && usedBy.length > 0 -> true

== removeModule('dep-node') ==
returned infoList entries: [{"id":"dep-node/dep","enabled":false,"types":["depType"]}]
moduleConfigs still contains 'dep-node': false
nodeConstructors still contains 'depType': false
nodeTypeToId still contains 'depType': false

The intended branch would have left moduleConfigs["dep-node"] in place (nodes stay loaded for my-nodes) and only set user = false.

Environment

  • Node-RED version: current main (5.0.8) — packages/node_modules/@node-red/registry/lib/registry.js:274
  • Node.js version: 26.x (the broken comparison is version-independent)
  • Platform/OS: any
  • npm version: n/a

Suggested fix: if (module.usedBy && module.usedBy.length > 0).

Activity

  1. cestercian commented on Oct 18, 2026

    @cestercian
    Contributor

    hey, i'd like to take this one. have a fix in mind (switch the guard to usedBy.length like getAllNodeConfigs does) plus a registry test

  2. added a commit that references this issue on Oct 18, 2026
    912437f
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