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

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:

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-fetchis also an ESM module and can be accessed by the function. This is becausenode-fetchexports adefaultexport, and thefunctionnode appears to only assign thedefaultexport to the variable while discarding any named exports.asyncexports adefaultexport, 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 thedefaultexport assigned.
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

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
JoshuaCWebDeveloper commentedon Jan 21, 2024
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)
Handle undefined default export when importing module
knolleary commentedon Jan 22, 2024
@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
defaultis not defined. That should be entirely backward compatible as any existing module that exportsdefaultwill 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.
JoshuaCWebDeveloper commentedon Jan 22, 2024
@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
Namedthat has all of the named exports. So for example:xyz-builderassigned to variablexyzBuilderdefaultexport gets assigned toxyzBuilderxyzBuilderNamedknolleary commentedon Jan 23, 2024
@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.