How can start a node at the start of NodeRed?

It is necessary that when Code RED starts, personal functions are loaded, i.e. that the code in the Function node is executed:

const utilityFunctions = {
    myFunction: function(x, y) {
        return x + y
global.set('utilityFunctions', utilityFunctions)

How to do it?

Have you tried the on start tab of the function node


A inject node, set to inject once at start and connect that to your function node.

Thanks for the help !
And where in the inject node "inject once at start" ?
I have not found:

The option you are looking for is hidden behind the list of options for the select box you have opened.

1 Like

Thank you,

Then it follows that:
"inject once at start" == "Inject once after 0.1 seconds, then"
Question 1: What delay do you recommend specifying ?

It is written in the help of the inject node: "It can also be configured to inject once each time the flows are started."
Question 2: What is the difference between flows are started and Node RED are started ?

If it's important that the inject occurs first, apparently you should have it on the first editor tab since they are executed from left to right.

This is just hearsay; it might be a myth.
Still, it can't hurt to put your initialisation flows on the first tab.

1 Like

Correct. When the flow file is deployed/saved it is written out in tab order. So when read/started the first tab gets loaded and run first. Normally a delay of .1 s is ok to allow connected nodes to also start, but in this case where the OP just wants to setup the environment the timeout can be set to 0

1 Like

Node red starts and then it starts the flows. If you start node-red by using
node-red --safe
it starts node-red but does not start the flows. In that mode the editor works but no flows are running.


To add to what others have said, the flows are also started each time you hit the deploy button (assuming you do a 'full' deploy and not one of the modified flows/nodes types of deploy).

1 Like

A further point. If you have code that relies on a context variable then it should always check that the variable has been setup before using it, and if it has not then take appropriate action, using a default, or doing nothing or whatever is appropriate.

1 Like

I wrote down all the answers above in my cheat sheet - I got a fragment for the treatise "NodeRed".
Thanks to all the participants !

Question 3: As I understood earlier, threads are triggered by events that occur. Why then is it said that threads are started at startup?

There is only one thread running in node red. It runs the editor and the flows.

Where is that said?

Sorry, but you wrote it yourself:
"Node red starts and then it starts the flows."

That is about node red, not 'threads'. Node red is a process of (generally) one thread. It provides the node red editor capability and also runs flows, all within that thread.

I completely agree with you.
It turns out that in the end it is necessary to write:
"The Node RED process starts, which makes it possible for events to run threads."
Is that right?

I would not say "events run threads".

There is a single thread in the JavaScript part of the node.js runtime - the event loop.

To talk of multiple threads can lead to misunderstanding an important core principle of node.js - or at least how Node-RED runs on top of node.js.

When Node-RED starts the flows, the nodes are able to schedule events on the event loop. That is how multiple pieces of work can happen at the "same" time - even if strictly speaking only one thing happens at any one time.

Deeper inside the node.js runtime, pieces of work such as network access or filesystem access may be happening in separate threads, but they will ultimately result in an event being scheduled back on the single node.js event loop thread.

Given your information, it may be more correct to formulate it like this:
"The Code RED process starts, which makes it possible for events (nodes) to run threads that are Editor elements together with this nodes."

No. There are not multiple threads. It is just wrong to say otherwise.

When Node-RED starts it does two things. First it starts an http server that serves up the editor, along with an http API used by the editor to load and modify the running flows. Second, the runtime 'runs' the current flow configuration by instantiating all of the nodes in the flow. This all happens within the single thread of the node.js event loop, as described in the link I shared in my last response

Excuse me. My level of knowledge does not allow me to assert anything. And even more so to argue.
I'm just trying to understand your phrase "flows are also started".
As a regular user, I use the terms node and Node RED thread, which I see in the editor. I'll learn the rest from you.
And that's why I don't understand what it means to run threads in the Editor.