Skip to content

Named exports from ESM modules not accessible in function nodes #4536

Closed

Description

@JoshuaCWebDeveloper

Current Behavior

An ESM module (in this case, sse.js) is defined as a module in the setup tab of a core function node:
image

The function attempts to access it like so in onMessage:

return { sseJs, nodeFetch, async }

However, the variable that sse.js is imported as will be undefined:
image

This is because sse.js exports no default export; however, it exports named exports that I need to access from the function.

Things to note:

  • node-fetch is also an ESM module and can be accessed by the function. This is because node-fetch exports a default export, and the function node appears to only assign the default export to the variable while discarding any named exports.
  • async exports a default export, but all named exports of async can also be accessed. This is because CommonJS modules are assigned to the variable in full, while ESM modules appear to only have the default export assigned.
image

Expected Behavior

I should have some way of accessing named exports from ESM modules, and an ESM module should not simply be undefined simply because it has no default export.

The most straightforward way of accomplishing this is to simply treat ESM modules the same as CommonJS modules, i.e. assign the full module to the variable, and if I want to access the default export, I would need to do nodeFetch.default the same as I currently need to do async.default. In addition to being the smallest lift, this has the added advantage of making this behavior more consistent across CommonJS and ESM. Unfortunately, it has the potential to break existing flows that are using the default export from ESM modules.

A more robust solution would be an overhaul of the modules UI that would allow me to specify how the module should be assigned to the variable (i.e. default export, named exports, or whole module).

Steps To Reproduce

Create a simple flow with a function node setup as described under Current Behavior
image

Example flow

[
    {
        "id": "3fb1b07be44ed756",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a861854fd38b3329",
        "type": "function",
        "z": "3fb1b07be44ed756",
        "name": "function 3",
        "func": "return { sseJs, nodeFetch, async }",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "sseJs",
                "module": "sse.js"
            },
            {
                "var": "async",
                "module": "async"
            },
            {
                "var": "nodeFetch",
                "module": "node-fetch"
            }
        ],
        "x": 360,
        "y": 160,
        "wires": [
            [
                "9ddadd7f7c581e24"
            ]
        ]
    },
    {
        "id": "19ec915f8e7ab75f",
        "type": "inject",
        "z": "3fb1b07be44ed756",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 140,
        "wires": [
            [
                "a861854fd38b3329"
            ]
        ]
    },
    {
        "id": "9ddadd7f7c581e24",
        "type": "debug",
        "z": "3fb1b07be44ed756",
        "name": "debug 7",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 180,
        "wires": []
    }
]

Environment

  • Node-RED version: 3.1.3
  • Node.js version: 16.20.2
  • npm version: 8.19.4
  • Platform/OS:
    Docker image: nodered/node-red:3.1.3
    Docker host: Linux 6e93128c6163 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 Linux
  • Browser: Microsoft Edge 120.0.2210.144 (Official build) (64-bit)

Activity

  1. JoshuaCWebDeveloper commented on Jan 21, 2024

    @JoshuaCWebDeveloper
    ContributorAuthor

    The workaround for this issue is to use a dynamic import from inside the function to import named exports; however, that in turn requires it's own workaround as detailed here: #4537 (comment)

  2. added a commit that references this issue on Jan 22, 2024
    c24f05c
  3. knolleary commented on Jan 22, 2024

    @knolleary
    Member

    @JoshuaCWebDeveloper thanks for highlighting another headache with ESM modules 😉

    I've applied a quick fix via #4539 that will fall back to exposing the whole module if default is not defined. That should be entirely backward compatible as any existing module that exports default will continue to be exposed as such and any that don't, couldn't be otherwise used.

    Let me know if this makes sense to you. We'll get this into Node-RED 3.1.4 soon.

  4. JoshuaCWebDeveloper commented on Jan 22, 2024

    @JoshuaCWebDeveloper
    ContributorAuthor

    @knolleary Thanks for looking at this!

    Your solution addresses my specific use case with this one module; however, there is also the use case of needing a named export from a module that does export a default. Many modules have additional named exports that aren't accessible from the default export.

    I think the solutions I outlined above are best; however, if you're looking for a 100% backwards compatible solution, perhaps you could create a second variable suffixed with Named that has all of the named exports. So for example:

    • I specify ESM module xyz-builder assigned to variable xyzBuilder
    • The default export gets assigned to xyzBuilder
    • The named exports get assigned (as an object) to xyzBuilderNamed
  5. knolleary commented on Jan 23, 2024

    @knolleary
    Member

    @JoshuaCWebDeveloper right - the PR isn't intended to fix it for every scenario, but is a quick iteration to unblock a particular set of modules. We still need to think about modules with both a default and other named exports.

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