Opening node-red

Hi, when I want to open node-red, I faced an error. Can you help me to resolve it??

TypeError: Cannot read property 'forEach' of undefined
at Object.addModule (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\registry\lib\registry.js:186:27)
at C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\registry\lib\loader.js:108:30
at tryCatchReject (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:845:30)
at runContinuation1 (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:804:4)
at Fulfilled.when (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:592:4)
at Pending.run (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:483:13)
at Scheduler._drain (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\Scheduler.js:62:19)
at Scheduler.drain (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\Scheduler.js:27:9)
at processTicksAndRejections (internal/process/task_queues.js:75:11)

How/where are you running Node-RED?

And have you installed any 3rd party (self developed) nodes before it failed?

when I type node-red in Node.js command prompt.

I receive this:

Welcome to Node-RED

25 Jan 10:10:58 - [info] Node-RED version: v1.2.7
25 Jan 10:10:58 - [info] Node.js version: v14.15.4
25 Jan 10:10:58 - [info] Windows_NT 10.0.19041 x64 LE
25 Jan 10:10:58 - [info] Loading palette nodes
25 Jan 10:11:01 - [error] Failed to start server:
25 Jan 10:11:01 - [error] TypeError: Cannot read property 'forEach' of undefined
at Object.addModule (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\registry\lib\registry.js:186:27)
at C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\registry\lib\loader.js:108:30
at tryCatchReject (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:845:30)
at runContinuation1 (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:804:4)
at Fulfilled.when (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:592:4)
at Pending.run (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\makePromise.js:483:13)
at Scheduler._drain (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\Scheduler.js:62:19)
at Scheduler.drain (C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules\when\lib\Scheduler.js:27:9)
at processTicksAndRejections (internal/process/task_queues.js:75:11)

Hi @Mas

how did you install Node-RED?

We have seen this error before on Windows, but I cannot immediately remember what caused it, other than it was an 'unusual' install.

Yes, I installed some, but know I do not remember which one was the last self developed node.

I install node-red on Windows. I was working well, and suddenly I faced this error!

That does suggest it will be related to one of the extra nodes you have installed.

Can you work backwards and uninstall each module in turn to see which broke things?

I have removed nodes which are called node-red-contrib-...
by entering npm remove node-red-contrib-...
But still, it does not work!

You have a couple of options at this point.

Possibly the easiest is to take a copy of your flows and credentials json files and put them somewhere safe. Then delete them from your userDir and restart Node-RED so that they are recreated from scratch.

Then you can import your saved flow but when you do so, delete as much as possible before you commit it. Then restart Node-RED again so that you know whether you have hit the issue. Of course, you will need to install nodes along the way. Just take it slow and steady so that you find where the issue lies.

I recently had a similar issue and it drove me mad because I thought it was being caused by a dependent module that I'd loaded. In fact, it was simply a missing global variable that had been deleted. What confused me was that the module I'd installed (not a node) was making extensive use of promises and it caused the error message stack to look rather odd.


The other thing you could do if you know how to do node.js debugging would be to temporarily put a breakpoint on the line of code in Node-RED that is reporting the error then you could step through things and find out which node is being loaded when the error occurs.

@knolleary should there be a test before that line of code to catch that error?

Looks like there should be something to test whether set.types exists or perhaps rather whether it is an array before trying to apply .forEach?

Another option, that is slightly less drastic, is to look in the file ~/.node-red/.config.nodes.json - this will give you a list of the extra nodes the runtime is still trying to load. Check through those to see if any of them are causing the issue.

Yes, but that would just mask the error. I can't remember what root cause we discovered for this last time - but it was sufficiently of a nature that I didn't rush to add error handling for it. It could have been a badly implemented custom node someone was working on locally. If I can track it down, I'll see where we got to on it.

I was thinking that the test would create set.err which other errors in the same area create so that something useful could be reported back.

It looks as though there is a node that hasn't defined any types. Presumably setName is the name of the thing being loaded and so could be reported back.

Small change to the addModule function would do the job. I've tested this and it works as expected (this from v1.2.7 code, I note that the dev branch is marginally different).

function addModule(module) {
    moduleNodes[module.name] = [];
    moduleConfigs[module.name] = module;
    for (var setName in module.nodes) {
        if (module.nodes.hasOwnProperty(setName)) {
            var set = module.nodes[setName];
            moduleNodes[module.name].push(set.name);
            nodeList.push(set.id);
            if (!set.err) {
                if ( ! Array.isArray(set.types) ) {
                    set.err = "Type not defined - check node definition";
                    set.err.code = "type_not_defined";
                    set.err.details = {
                        moduleB: set.module
                    }
                } else {
                    set.types.forEach(function(t) {
                        if (nodeTypeToId.hasOwnProperty(t)) {
                            set.err = "Type already registered";
                            set.err.code = "type_already_registered";
                            set.err.details = {
                                type: t,
                                moduleA: getNodeInfo(t).module,
                                moduleB: set.module
                            }

                        }
                    });
                }
                if (!set.err) {
                    set.types.forEach(function(t) {
                        nodeTypeToId[t] = set.id;
                    });
                }
            }
        }
    }
    if (module.icons) {
        icon_paths[module.name] = icon_paths[module.name] || [];
        module.icons.forEach(icon=>icon_paths[module.name].push(path.resolve(icon.path)) )
    }
    if (module.examples) {
        library.addExamplesDir(module.name,module.examples.path);
    }
    nodeConfigCache = {};
}

It was an opportunity to get more familiar with a bit more of the node-red core.

If you carefully and temporarily replace the addModule function in C:\Users\Admin\AppData\Roaming\npm\node_modules\node-red\node_modules@node-red\registry\lib\registry.js with my code above, you should find out what module is causing the issue.

You can leave the amended code in place, it will be replaced when Node-RED is next updated.

Thank you for your help and advice. Sorry, as I am new in node-red, I got confused.
Now, I need to replace registery.js with your code addModule?
registery.js code is very long, do I have to replace fully or a part of that?

No, you only have to find the addModule function and replace it with mine.

BTW, if you get stuck or mess it up, don't panic. You can always just do npm install node-red -g --unsafe-perm --production to overwrite your changes with the original.

I appreciate your help. I have done that and I faced some errors in registery.js.
for instance

when I run node-red, I received this:
C:\Users\Admin>node-red
28 Jan 09:41:07 - [info]

Welcome to Node-RED

28 Jan 09:41:07 - [info] Node-RED version: v1.2.7
28 Jan 09:41:07 - [info] Node.js version: v14.15.4
28 Jan 09:41:07 - [info] Windows_NT 10.0.19041 x64 LE
28 Jan 09:41:08 - [info] Loading palette nodes
28 Jan 09:41:11 - [info] Dashboard version 2.25.0 started at /ui
28 Jan 09:41:11 - [warn] ------------------------------------------------------
28 Jan 09:41:11 - [warn] [node-red-contrib-netio/sample] Type not defined - check node definition
28 Jan 09:41:11 - [warn] ------------------------------------------------------
28 Jan 09:41:11 - [warn] Missing node modules:
28 Jan 09:41:11 - [warn] - node-red-contrib-simpletime (2.10.0): simpletime
28 Jan 09:41:11 - [warn] - node-red-contrib-homekit-abc (0.0.26): hassbian-abc-homekit-bridge, hassbian-abc-homekit-broadlink-mp1, hassbian-abc-homekit-windowCovering, hassbian-abc-homekit-midehumidifier, hassbian-abc-homekit-mihumidifier, hassbian-abc-homekit-haier-airbox, hassbian-abc-homekit-yeelight, hassbian-abc-homekit-miheater, hassbian-abc-homekit-camera, hassbian-abc-homekit-irmqtttv, hassbian-abc-homekit-mqttAC, hassbian-abc-homekit-qlwzdc1, hassbian-abc-homekit-zdc1, hassbian-abc-homekit-zm1, hassbian-abc-homekit-ztc1
28 Jan 09:41:11 - [warn] - node-red-contrib-contextbrowser (0.0.4): contextbrowser, contextbrowser-sidebar
28 Jan 09:41:11 - [warn] - node-red-contrib-modbus (5.13.3): modbus-client, modbus-response, modbus-read, modbus-getter, modbus-flex-getter, modbus-write, modbus-flex-write, modbus-server, modbus-flex-server, modbus-queue-info, modbus-flex-connector, modbus-io-config, modbus-response-filter
28 Jan 09:41:11 - [warn] - node-red-contrib-cpu (0.0.4): cpu
28 Jan 09:41:11 - [warn] - node-red-contrib-johnny5 (0.50.0): gpio in, gpio out, ioplugin, script
28 Jan 09:41:11 - [warn] - node-red-contrib-tplink (1.0.0): kasa
28 Jan 09:41:11 - [warn] - node-red-contrib-hs100 (0.4.0): hs100
28 Jan 09:41:11 - [warn] - node-red-contrib-dlink-dspw (1.0.1): dsp-w215
28 Jan 09:41:11 - [warn] - node-red-contrib-virtualmeter (0.5.28): virtualmeter, summeter, loadprediction, pvprediction, sumprediction, simplegsi
28 Jan 09:41:11 - [warn] - node-red-contrib-homeseer (1.0.5): hs-server, hs-device, hs-event
28 Jan 09:41:11 - [warn] - node-red-contrib-homekit (0.2.1): homekit-service, homekit-accessory
28 Jan 09:41:11 - [warn] - node-red-contrib-net-ping (1.0.0): net-ping-node
28 Jan 09:41:11 - [warn] - node-red-contrib-uibuilder (3.0.1): uibuilder
28 Jan 09:41:11 - [warn] - node-red-contrib-blockly (1.1.0): Blockly
28 Jan 09:41:11 - [warn] - node-red-contrib-timerswitch (1.3.0): timerswitch
28 Jan 09:41:11 - [warn] - node-red-contrib-countdown (1.3.1): countdown
28 Jan 09:41:11 - [warn] - node-red-contrib-toggle (0.1.1): toggle
28 Jan 09:41:11 - [warn] - node-red-contrib-image-output (0.6.2): image
28 Jan 09:41:11 - [warn] - node-red-contrib-hello-world (0.0.5): hello-world
28 Jan 09:41:11 - [info] Removing modules from config
28 Jan 09:41:11 - [info] Settings file : C:\Users\Admin.node-red\settings.js
28 Jan 09:41:11 - [info] Context store : 'default' [module=memory]
28 Jan 09:41:11 - [info] User directory : \Users\Admin.node-red
28 Jan 09:41:11 - [warn] Projects disabled : editorTheme.projects.enabled=false
28 Jan 09:41:11 - [info] Flows file : \Users\Admin.node-red\flows_DESKTOP-GC83620.json
28 Jan 09:41:11 - [info] Server now running at http://127.0.0.1:1880/
28 Jan 09:41:11 - [warn]


Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.

28 Jan 09:41:11 - [info] Waiting for missing types to be registered:
28 Jan 09:41:11 - [info] - modbus-client
28 Jan 09:41:11 - [info] - Blockly
28 Jan 09:41:11 - [info] - countdown
28 Jan 09:41:11 - [info] - toggle

There you go, that is the issue.

Great. Could you please tell me how to resolve this issue?

Actually, I was trying to create a node, I followed the document in this website Creating Nodes : Node-RED
basically, we should use " node-red-contrib-" as a prefix to name. which I called it " node-red-contrib-netio". I have started with a simple example of showing the text. Because I was amateur in this field.
I have done all steps and I have added the node in the node-red library. it was added in the library, however, when I search it to install in the node-red, it was not found.

Is that your node? Or someone else's?

If it is yours, you need to go back to the docs to read again all of the things you need to do to have a successful custom node. In this case, the node called "sample" is missing the node type definition.

If it is not your node, you need to contact the node's author.

@Mas I see you have indeed published your node to the library - node-red-contrib-netio (node) - Node-RED

We generally ask that people don't publish nodes to the library until they have been tested and are working. Given you knew this node wasn't working, then publishing it to npm and the library wasn't ideal.

Looking at your node, it is missing the node's .html file - as described in the docs you said you followed: Creating your first node : Node-RED