How to set env variable

env.get() working
but env.set() is not a function like flow.set(),global.set()ext,

so please find me how to set env variable

You cannot set environment variables at runtime. They are only evaluated when flows are started.

then please told me how to do that

i am pass one variable from front end ok then i used that value into some nodes as from knowledge nodes suporting only env varibales so i want to set that front end varible to one env varibale .. so what can i do for that

One way of setting environment variables is in your settings.js file.

The following will set the env var MY_ENV_VAR:

process.env.MY_ENV_VAR = "Hello World";

You can add lines like that to the top of your settings file.

The other option is to set them completely outside of Node-RED in shell you are running Node-RED in. How you do that will depend on your OS and how you are running Node-RED.

no this way is also looking like a static ri8 .. i just want a dynamic way

then why cant use like this inside function

process.env.myvar = "abc";

error: precess is not defined;

You cannot change env vars dynamically. We do not expose the process object in the Function node. That is just how it is.

2 Likes

@DemonKing if you tell us which node or nodes you are trying to change at run time we may be able to help.

Node-red-contrib-mssql- plus

Which fields?

All fields of conection like ip dbname everything I want to use dynamic

I haven't examined the code of that node, but I imagine it is written to connect to the database on startup and does not have the code to switch to a different db. Therefore even if you could change environment vars at runtime the node does not have the code to disconnect from one and connect to another.
What you are asking for is a new feature of the node, not of node-red.

Yes I think so.. but in my project I had this problem.. we are using entire project for node red.... So what's the solution for my problem .. please let me know

But I hope if I can change the env variable run time it's should be work for me.. bcz If I give. A env variable given for the ipout for that node.. it's working fine

@DemonKing environment variables are only evaluated when flows are started. The runtime gets their values before passing them to individual nodes. The nodes don't know the value came from an env var.

If you change an env var it does not magically cause the node to change its configuration. You'd need to restart Node-RED to pickup the changes.

I understand you have a requirement to dynamically change the mssql connection details. Unless there is a node that already provides that ability, then you are short of options other than creating a custom node of your own.

(Another of my dumb questions)

Though I am not using data bases and that stuff.

Why an env variable anyway? Why can't a global variable be used?
That seems more the right thing for the job.

If it needs to be changed and all to see it - in all flows - then surly a global variable (context) is just what the doctor ordered.

I lready tried global variables but I am pretty sure node config allows env variables only

Could you tell me how to create a custom nodes .. just told me the option how to do that

Hey, I'm not expert, but that doesn't sound right.

As I said, I don't use databases. I some how can't get my head around them and I have been trying for 30 years - there about.

Explain the mechanics of what you are doing. I am looking at your second post and can't work out what you really mean.

Node-Red must be able to use the data base, otherwise there wouldn't be a node for it.

I can't believe that settings which allow/need to be changed can't be access from Node-Red's working system.

So? Use DNS and access it by the name rather than the IP.

Your local network would/should have a DNS server.

Somewhere in your RPI settings you can get it to also publish (bad word, but....) its name and IP together.

That way rather than needing to enter the IP address, you enter the name.
The name gets sent to the DNS and gets translated to the IP.

Problem solved - I think.
Or it should be to the best of my knowledge.

The possible use of global vars is a red herring. The problem is not whether the node is capable of getting config from global or env vars, it is whether the node can change its connection to a different database without restarting or deploying node red, which it apparently can't.