# Using Environment Variables in settings.js

**URL:** <https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295>\
**Category:** General\
**Created:** [10 August 2022 17:06 UTC](https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295 "2022-08-10T17:06:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stokes776](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stokes776/32/66036_2.png) [@stokes776](https://discourse.nodered.org/u/stokes776)\
**Post date:** [10 August 2022 17:06 UTC](https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295/1 "2022-08-10T17:06:57Z")

</div>

Hello,

I am trying to dynamically set my pageTitle.

```auto
    "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?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 August 2022 18:18 UTC](https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295/2 "2022-08-10T18:18:16Z")

</div>

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 (nodejs.dev)](https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 August 2022 19:44 UTC](https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295/3 "2022-08-10T19:44:52Z")

</div>

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.

```auto
#! /bin/bash
# Change node-red editor page header to show hostname & IP. Make dashboard browser tab show hostname
############################################
DIR=/home/pi/.node-red

# 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?
IP=/home/pi/bin/ipaddress
test -x $IP || { echo "This program needs (missing) $IP - quitting." ; exit 1; } # {} groups commands, need ; after last command.

checkfile()
{
  file=$1; string=$2
  if test ! -f $file
  then
     echo $file not found; return 1
  elif grep $string $file >/dev/null
  then
     echo $string is already in $file - Not changed
     return 1
  fi
}

echo "=============================================== settings.js ..." # 2 insertions, above module.exports and below editorTheme
FILE=$DIR/settings.js
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 "// --------------------------------------"
                                          print
                                      }
        else if ($0 ~ /editorTheme: {/) {
                                          print
                                          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
echo
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
FILE=$DIR/$FLOWFILE
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
then
   echo $FILE was not changed.
   rm $FILE.bak
else
   echo $FILE was changed. Original saved as $FILE.bak
fi
echo

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! 🙃 )

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

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

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

```

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

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b8aed3085f287c90ae75ef825b2c11604dc882d.jpeg)  
to this  
 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c0415566393431ab9b6f24a358d707b163adaacd.jpeg)

---

<div class="post-metadata">

**Author:** ![stokes776](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stokes776/32/66036_2.png) [@stokes776](https://discourse.nodered.org/u/stokes776)\
**Post date:** [11 August 2022 15:41 UTC](https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295/4 "2022-08-11T15:41:00Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/608766675d3cf7a73d4a02fce54d72ce0b0608f6.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 August 2022 15:41 UTC](https://discourse.nodered.org/t/using-environment-variables-in-settings-js/66295/5 "2022-08-25T15:41:44Z")

</div>

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