Skip to content

Dynamic imports do not work inside function nodes, ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING #4537

Closed

Description

@JoshuaCWebDeveloper

Current Behavior

A function node with the following onMessage body will result in an error:

(async () => {
    try {
        const sseJs = await import('sse.js');
        node.send({sseJs})
    }
    catch (e) {
        node.send({ e })
    }
})();

return msg;

The following error with stacktrace (remove await and try/catch above to see the full error in the terminal):

21 Jan 23:22:34 - [error] TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at new NodeError (node:internal/errors:387:5)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9)
    at Function node:b798c5038b11f7c6 [function 4]:4:23
    at Function node:b798c5038b11f7c6 [function 4]:10:3
    at Function node:b798c5038b11f7c6 [function 4]:14:3
    at Script.runInContext (node:vm:141:12)
    at processMessage (/usr/src/node-red/node_modules/@node-red/nodes/core/function/10-function.js:419:33)
    at FunctionNode._inputCallback (/usr/src/node-red/node_modules/@node-red/nodes/core/function/10-function.js:342:17)
    at /usr/src/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:214:26
    at Object.trigger (/usr/src/node-red/node_modules/@node-red/util/lib/hooks.js:166:13)

Expected Behavior

I should be able to dynamically import modules.

Use case:
I needed to do this due to deficiencies in the current module functionality of the function node (reported in #4536).

This is similar to this old issue: #41

Steps To Reproduce

Run the example flow.

Example flow

[
    {
        "id": "9248bd84dd1942e1",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "b798c5038b11f7c6",
        "type": "function",
        "z": "9248bd84dd1942e1",
        "name": "function 4",
        "func": "(async () => {\n    try {\n        const sseJs = await import('sse.js');\n        node.send({sseJs})\n    }\n    catch (e) {\n        node.send({ e })\n    }\n})();\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "sseJs",
                "module": "sse.js"
            },
            {
                "var": "async",
                "module": "async"
            },
            {
                "var": "nodeFetch",
                "module": "node-fetch"
            }
        ],
        "x": 400,
        "y": 140,
        "wires": [
            [
                "b32418793cf5f513"
            ]
        ]
    },
    {
        "id": "57e98886f5bbfd47",
        "type": "inject",
        "z": "9248bd84dd1942e1",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 120,
        "wires": [
            [
                "b798c5038b11f7c6"
            ]
        ]
    },
    {
        "id": "b32418793cf5f513",
        "type": "debug",
        "z": "9248bd84dd1942e1",
        "name": "debug 8",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 570,
        "y": 160,
        "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

    I do have a workaround for this issue, but it requires modifying the settings.js file:

        functionGlobalContext: {
            // os:require('os'),
            dynamicallyImport:(
                async (it) => {
                    try {
                        return await import(it)
                    }
                    catch (err) {
                        throw `Caught error: ${err}`;
                    }
                }),
        },

    dynamicallyImport can then be called in a function node like so:

    (async () => {
        try {
            const sse = await (global.get("dynamicallyImport"))("sse.js");
            node.send({sse});
            node.done();
        }
        catch (e) {
            node.send({ e });
        }
    })();
    
    return msg;
  2. knolleary commented on Jan 22, 2024

    @knolleary
    Member

    Given we don't support directly requireing modules in the Function node, this same rule should apply to using import in any of its guises.

    Rather than spend time fixing this workaround, we'll focus on the other issue #4536 to allow modules with a default export to be used.

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