Skip to content

Cannot import ESM modules in function nodes #3627

Closed

Description

@fungiboletus

Current Behavior

It seems that ESM modules cannot be loaded and have an not found error.

[error] [function:fcc94efea68c95c1] Error: Cannot find module '/Users/username/.node-red/node_modules/got'
Require stack:
- /Users/username/stuff/node_modules/@node-red/registry/lib/externalModules.js
- /Users/username/stuff/node_modules/@node-red/registry/lib/registry.js
- /Users/username/stuff/node_modules/@node-red/registry/lib/index.js
- /Users/username/stuff/node_modules/@node-red/runtime/lib/nodes/index.js
- /Users/username/stuff/node_modules/@node-red/runtime/lib/index.js
- /Users/username/stuff/node_modules/node-red/lib/red.js
- /Users/username/stuff/node_modules/node-red/red.js

Expected Behavior

I find the CommonJS/ESM situation to be a disaster, but a few nice modules are ESM only and their maintainers don't want to do CommonJS anymore. Forking all of these modules to support CommonJS may require more effort than supporting ESM in various tools such as Node-Red.

Steps To Reproduce

Install an ESM module, like got using npm i got and import it in a function node. It will fail to load. A CommonJS module fork, got-cjs will load successfully.

Example flow

[
    {
        "id": "fcc94efea68c95c1",
        "type": "function",
        "z": "cfed13ad6700d124",
        "name": "",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "got",
                "module": "got"
            }
        ],
        "x": 240,
        "y": 120,
        "wires": [
            []
        ]
    }
]

Environment

  • Node-RED version: 2.2.2
  • Node.js version: 18.2.0
  • npm version: 8.9.0
  • Platform/OS: Mac
  • Browser: NA

Activity

  1. knolleary commented on May 25, 2022

    @knolleary
    Member

    I find the CommonJS/ESM situation to be a disaster,

    100% agree. It is a complete headache for projects that have to cater for both. I find the approach of some modules to cut off from CommonJS entirely to be counter productive - why force people to rewrite their entire stack just because you don't want to add some meta data to your package.json to support both formats (I know that's an over simplification... but you get my point).

    Now, all that said, I did spend some time when we added the External Modules work to the Function node to get it working with both formats - using dynamic imports that worked happily across both.

    That said, I know I didn't test with got specifically, so it may be doing something our testing didn't cover.

  2. added a commit that references this issue on Jun 10, 2022
    71a272f
  3. added a commit that references this issue on Jun 13, 2022
    bda8e86
  4. strbaknxp commented on Nov 14, 2025

    @strbaknxp

    Hello, this is not working anymore on new Node-Red v4. Previously on version 3 or earlier it worked fine.
    Is here another way how to load properly ESM modules?

    my setup:
    NodeJS v24.11.1
    Node-Red: 4.0.9
    Windows 10 environment.
    ESM Library https://github.com/jsonquerylang/jsonquery

    Thank you

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