Using environmental variables (system or user defined)

Environmental variables (enter ‘env’ in a terminal to see which can be used) can be accessed by adding:

env: process.env

to the ‘functionGlobalContext:’ section of the settings.js file. Once entered you can access them in any node that has a text field (a/z) or in a function node.

SPECIAL CASES: If you want to use HOSTNAME or declare your own environmental variable, you must make an additional change to the settings.js file. Add the following lines just before the line ‘module.exports = {‘:

process.env.HOSTNAME = require('os').hostname();
process.env.FOO = 'just another bar';

Once you have made the changes to the settings.js file, stop/restart node-red to make them avaliable for use.

Example: to use in a text (a/z) field such as an INJECT node. use the format: ${LOGNAME} (Note: in newer versions of Node-RED, the Inject now has an option to set the payload to '$ env variable' - Just use the name of the variable like 'LOGNAME'.

To use in a FUNCTION use the following:

var env = global.get('env');
msg.payload = env.LOGNAME;

Other examples are in the flows.

[{"id":"dbfdd28e.d60f","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"5f0514d3.9aba74","type":"inject","z":"dbfdd28e.d60f","name":"Test ${LOGNAME}","topic":"","payload":"${LOGNAME}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":150,"y":120,"wires":[["7a0721b4.ae0be"]]},{"id":"f9f1872f.fa0d6","type":"debug","z":"dbfdd28e.d60f","name":"test 3","active":true,"console":"false","complete":"true","x":650,"y":240,"wires":[]},{"id":"2bc90895.53dbb8","type":"inject","z":"dbfdd28e.d60f","name":"Test ${FOO}","topic":"","payload":"${FOO}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":130,"y":240,"wires":[["f9f1872f.fa0d6"]]},{"id":"e0374b07.e54ec","type":"inject","z":"dbfdd28e.d60f","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":110,"y":300,"wires":[["ff27a66a.07cde8"]]},{"id":"ff27a66a.07cde8","type":"function","z":"dbfdd28e.d60f","name":"Use env.get(\"FOO\") in a function","func":"//var env = global.get('env');\nmsg.payload = env.get(\"FOO\");\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":300,"wires":[["aa22d8a5.06c008"]]},{"id":"aa22d8a5.06c008","type":"debug","z":"dbfdd28e.d60f","name":"test 4","active":true,"console":"false","complete":"true","x":650,"y":300,"wires":[]},{"id":"f08f319e.a12e4","type":"inject","z":"dbfdd28e.d60f","name":"Test ${HOSTNAME}","topic":"","payload":"${HOSTNAME}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":150,"y":180,"wires":[["c73c66d3.19839"]]},{"id":"7a0721b4.ae0be","type":"debug","z":"dbfdd28e.d60f","name":"test 1","active":true,"console":"false","complete":"true","x":650,"y":120,"wires":[]},{"id":"c73c66d3.19839","type":"debug","z":"dbfdd28e.d60f","name":"test 2","active":true,"console":"false","complete":"true","x":650,"y":180,"wires":[]},{"id":"6729b0b3.7e7ac","type":"inject","z":"dbfdd28e.d60f","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":360,"wires":[["d88711ed.141798"]]},{"id":"f73e7363.b7b2d","type":"debug","z":"dbfdd28e.d60f","name":"test 5","active":true,"console":"false","complete":"true","x":650,"y":360,"wires":[]},{"id":"d88711ed.141798","type":"change","z":"dbfdd28e.d60f","name":"Use ${FOO} in a change","rules":[{"t":"set","p":"payload","pt":"msg","to":"${FOO}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":360,"wires":[["f73e7363.b7b2d"]]},{"id":"7efbc5.9e07dc3c","type":"switch","z":"dbfdd28e.d60f","name":"Check payload for loged in user","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"${LOGNAME}","vt":"str"},{"t":"neq","v":"${LOGNAME}","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":410,"y":440,"wires":[["f3f4cb65.6c45e8"],["db49f300.b7ebc"]]},{"id":"30c4c72d.02104","type":"comment","z":"dbfdd28e.d60f","name":"Before using you must make changes to settings.js - see the info bar for details.","info":"Environmental variables (enter 'env' in a terminal to see which can be used) can be accessed by adding 'env:require(env)' to the 'functionGlobalContext:' section of the settings.js file. Once entered you can access them in any node that has a text field (a/z) or in a function node.\n\nFor example: to use in a text (a/z) field such as an INJECT node. use the format: $(LOGNAME). (Note: in newer versions of Node-RED, the Inject now has an option to set the payload to '$ env variable' - Just use the name of the variable like 'LOGNAME'.\n\nTo use in a FUNCTION use the following:\n\tvar env = global.get('env');\n\tmsg.payload = env.LOGNAME;\n\nSPECIAL CASES:\nIf you want to use HOSTNAME or declare your own environmental variable, you must make an additional change to the settings.js file. Add the following lines just before the line 'module.exports = {':\n\tprocess.env.HOSTNAME = require('os').hostname();\n\tprocess.env.FOO = 'just another bar';\n\nOnce you have made the changes to the settings.js file, stop/restart node-red to make them avaliable for use.\nYou can now use either as explained above. Examples are in the flows.\n","x":420,"y":60,"wires":[]},{"id":"d605f74f.cbfe78","type":"inject","z":"dbfdd28e.d60f","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":520,"wires":[["ffeda30.4c3a0e"]]},{"id":"ffeda30.4c3a0e","type":"function","z":"dbfdd28e.d60f","name":"Use env.get(\"LOGNAME\") in a function","func":"msg.payload = env.get(\"LOGNAME\");\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":520,"wires":[["15f974a.743560b"]]},{"id":"15f974a.743560b","type":"debug","z":"dbfdd28e.d60f","name":"test 8","active":true,"console":"false","complete":"true","x":650,"y":520,"wires":[]},{"id":"a593e6f2.2abbb","type":"inject","z":"dbfdd28e.d60f","name":"Use $(LOGNAME)","topic":"","payload":"$(LOGNAME)","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":420,"wires":[["7efbc5.9e07dc3c"]]},{"id":"67433ca0.b8f6dc","type":"inject","z":"dbfdd28e.d60f","name":"Use JohnDoe","topic":"","payload":"JohnDoe","payloadType":"str","repeat":"","crontab":"","once":false,"x":130,"y":460,"wires":[["7efbc5.9e07dc3c"]]},{"id":"f3f4cb65.6c45e8","type":"debug","z":"dbfdd28e.d60f","name":"test 6 - true","active":true,"console":"false","complete":"true","x":670,"y":420,"wires":[]},{"id":"db49f300.b7ebc","type":"debug","z":"dbfdd28e.d60f","name":"test 6 - false","active":true,"console":"false","complete":"true","x":670,"y":460,"wires":[]}]

Flow Info

Created 7 years, 8 months ago
Updated 4 years, 10 months ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • comment (x1)
  • debug (x8)
  • function (x2)
  • inject (x8)
  • switch (x1)
Other
  • tab (x1)

Tags

  • environmental
  • variables
  • process
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option