Can I set the initialization value of node properties through environment variables?

Can I set the initialization value of node properties through environment variables?

init

Some yes - from the documentation...

Any node property can be set with an environment variable by setting its value to a string of the form ${ENV_VAR} . When the runtime loads the flows, it will substitute the value of that environment variable before passing it to the node.

This only works if it replaces the entire property - it cannot be used to substitute just part of the value. For example, it is not possible to use CLIENT-${HOST} .

As nodes provide their own edit dialog, not all properties will provide a text input that can be used to enter the env-var string. In that case, you may consider hand-editing the flow file to set the property.

https://nodered.org/docs/user-guide/environment-variables

Thanks a lot!
I tried all the ways but I can't

RED.nodes.registerType("my-node", {
    color: "rgb(231, 231, 174)",
    category: "test",
    defaults: {
      name: { value: `${global_name}` }
    },

Hi. It wasn't clear you meant as a programmer. I thought you meant as a user.

See the below link for accessing node env vars ...

"How to read environment variables from Node.js" https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs

Yes, I tried nodejs env (process.env.global_name) in file my_node.js ok but try in my_node.html it doesn't work.
Browser error 'process is not defined'

RED.nodes.registerType("my-node", {
    color: "rgb(231, 231, 174)",
    category: "test",
    defaults: {
      name: { value: `${process.env.global_name}` }
    },

There is no need for the template literal, you can use the value directly.

Try this...

console.log("process.env.global_name", process.env.global_name);
RED.nodes.registerType("my-node", {
    color: "rgb(231, 231, 174)",
    category: "test",
    defaults: {
      name: { value: process.env.global_name}
    },

And see what the console reports.

Ps. Are you sure the terminal / service / env you use to start node-red actually has global_name set?

Edit.....

Aaaaahhhhhhhggggggghhhhhhh

I just realised. This is client side code.

The browser will never have access to either the users computer or the node-red servers environment.

Yes, it's client side, sorry for not making this clear

There is kinda a way.

You create an admin endpoint in the js file then call that endpoint in onedirprepare client side code. The server-side code can then access and return vars that reside in the servers environment.

Look at node-red src code in the serial node for and example of how to.

oneditprepare() only works when double-click to open node. In this case I just select node and look at the node property, that function is not called, so this way is not fixable

Correct, but why is that a problem. Typically when a user adds a node into the editor they open it to set up.

I am doing the following way

RED.events.on("view:selection-changed", function (selections) {
Call to server to get global_name = process.env.global_name
Set my_node.global_name = global_name;
RED.sidebar.info.refresh();
});

I checked it was running but I don't know if there is any risk

I see the problem is this event is called too many times, do you know another event to determine the node is being initialized?
EDIT
I decided to use the event "nodes:add" and I think it is ok

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.