Pass ENV variable from OS into NodeRed (from Docker...)

1,184 views
Skip to first unread message

Jéan Roux

unread,
Jul 27, 2017, 3:16:44 AM7/27/17
to Node-RED
Hi, with Docker - in the compose yaml file, one can set environment variables (ENV) for the node that starts up. 

I see several discussions previously on the need / request for the ability to pass O/S ENV variables into flows, but not sure from reading them, that this is possible, also found no flows that could offer this capability.

In my use case, I fire up instances of NR in Docker - as front end client to backend processes, each client app I currently config with a config.cfg file, where I hold a json key pair sting, which then configures the front-end interface, using NR ui for that.

Would be cool if I rather - in the yaml compose file, pass this json string into the OS as an ENV, and then - NR as it starts up, pick up the ENV variable from the OS, and configures the front end client, one less thing that can go wrong.

Is it possible to read an OS ENV variable from within NR ?

Ivan Tarozzi

unread,
Jul 27, 2017, 3:55:40 AM7/27/17
to Node-RED
Hi Jéan,
you can solve using a javascript function node with this content:

  msg.payload = context.global.process.env.JAVA_HOME
  return msg;

But you need to modify your ~/.node-red/settings.js in this section, adding process module:

 functionGlobalContext: {
               os:require('os'), 
               process:process 
   },


Jéan Roux

unread,
Jul 27, 2017, 5:57:22 AM7/27/17
to node...@googlegroups.com
I will try it, thank you!

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/TnLyfQxy97s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/cbb26775-4027-409f-91fe-798f6811729a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathanaël Lécaudé

unread,
Jul 27, 2017, 10:24:56 AM7/27/17
to Node-RED
In most configuration fields you can also use this notation to access environment variables:
$(MY_VARIABLE)

Ivan Tarozzi

unread,
Jul 27, 2017, 11:10:50 AM7/27/17
to Node-RED
Il giorno giovedì 27 luglio 2017 16:24:56 UTC+2, Nathanaël Lécaudé ha scritto:
In most configuration fields you can also use this notation to access environment variables:
$(MY_VARIABLE)

 

Wonderful! I didn't know that way. It works without modification to settings.js file!  Thank you very much.


Jéan Roux

unread,
Jul 28, 2017, 7:33:09 PM7/28/17
to Node-RED
Thank you Ivan, this works.
Reply all
Reply to author
Forward
0 new messages