Using Environment Variables in settings.js


I am trying to dynamically set my pageTitle.

    "editorTheme": {
        "page": {
            "title": "${BALENA_DEVICE_NAME_AT_INIT}"

I use Balena, and it has an environment variable, that is accessible from within the Node-Red container named BALENA_DEVICE_NAME_AT_INIT.

I tried inserting the environment variable like I have done before in the flows.json, but when its in the settings.js it doesnt seem to work.

Is there a way to use Environment Varaibles within the settings.js?

settings.js is a node.js file so you need to use the node libraries to access the env vars.

How to read environment variables from Node.js (

1 Like

No idea if this will work with Balena, but I use this script to change the editor page header and browser tab title for editor and dashboard. I hope its some use to you.

#! /bin/bash
# Change node-red editor page header to show hostname & IP. Make dashboard browser tab show hostname

# Uses gawk. Probably not needed but a useful catch before running on an unknown system
gawk -V >/dev/null || { echo Please install gawk; exit 1; }
# Does /home/pi/bin/ipaddress exist?
test -x $IP || { echo "This program needs (missing) $IP - quitting." ; exit 1; } # {} groups commands, need ; after last command.

  file=$1; string=$2
  if test ! -f $file
     echo $file not found; return 1
  elif grep $string $file >/dev/null
     echo $string is already in $file - Not changed
     return 1

echo "=============================================== settings.js ..." # 2 insertions, above module.exports and below editorTheme
checkfile $FILE "process.env.HOSTNAME" || exit 1
cp $FILE $FILE.bak || exit 1
gawk ' { if ($0 ~ /^module.exports =/) {
                                          print "// ----------- added by script ----------"
                                          print " hostname = require('\''os'\'').hostname();"
                                          print " const execSync = require('\''child_process'\'').execSync;"
                                          print " const ipaddress = execSync('\''/home/pi/bin/ipaddress'\'', { encoding: '\''utf-8'\'' });"
                                          print " device = hostname + '\'' '\'' + ipaddress;"
                                          print " process.env.HOSTNAME = hostname;"
                                          print " process.env.DEVICE = device;"
                                          print "// --------------------------------------"
        else if ($0 ~ /editorTheme: {/) {
                                          print "// ----------- added by script ----------"
                                          print "       header: {"
                                          print "          title: process.env.DEVICE"
                                          print "       },"
                                          print "       page: {"
                                          print "          title: process.env.DEVICE"
                                          print "       },"
                                          print "// --------------------------------------"
        else print
      }' $FILE > $FILE.tmp
echo $FILE was changed. Original saved as $FILE.bak
mv $FILE.tmp $FILE || exit 1

FLOWFILE=$(grep "^[[:space:]]*flowFile:" $FILE | cut -d "'" -f2)  # flows file may be defined in settings.js
if [ "$FLOWFILE" == "" ]; then FLOWFILE=flows_$HOSTNAME.json ; fi # default value
echo "=============================================== $FLOWFILE ..." # Make dashboard tab on browser = hostname

checkfile $FILE HOSTNAME || exit 1
cp $FILE $FILE.bak || exit 1
sed -i s/"Node-RED Dashboard"/'${HOSTNAME}'/ $FILE
if  cmp $FILE $FILE.bak >/dev/null
   echo $FILE was not changed.
   rm $FILE.bak
   echo $FILE was changed. Original saved as $FILE.bak

echo Finished. Restart node-red to activate changes.

(I think the change in the flows file is only for the dashboard, and I'm not convinced it's working! :upside_down_face: )

Oh - It calls /home/pi/bin/ipaddress:

#! /bin/bash
# Print the eth0 else wlan0 IP address

for IFACE in eth0 wlan0
   IP=$(ifconfig $IFACE 2>/dev/null | awk ' /broadcast/ {print  $2 }' )
   if [ "$IP" ]
      echo $IP
      exit 0

On my Pi with HOSTNAME = "Crucial" It changes appearance from this

to this


Thank you guys, I had not even considered changing that header within the page, that is something I will add in as well.

For anyone coming across this in the future, this is what worked for me, that I will expand upon:

My settings.js is very small, only containing the things I need.

setting a var at the top, and then using that within the JSON, the trick to access the ENV variable, as mentioned by someone who replied up above, is process.env.ENVIRONMENTVARIABLENAME

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