Use a OS variable

I like to use a os variable in my flow, the hostname.

So i added a line in (top) of settings.js
(Nodered is installed as a docker container on a rpi 4b, with the latest image/container of NR)
process.env.hostname = require('os').hostname();

in my flow i use
var theHost = global.get('env').hostname();
msg.payload = theHost;

and get: TypeError: Cannot read property 'hostname' of undefined

I also tried

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

same problem

Would in not be var theHost = env.get("hostname")?

Tried this (no os var but a self decalred var
//added to top settings.js --> process.env.MY_ENV_VAR = "Hello World";

var theHost = env.get("MY_ENV_VAR")

return msg;

but no hello world, but strange content

return msg;


Almost there!
Added this in settings.js
process.env.MY_ENV_VAR = "Hello World";

functionGlobalContext: {

and in the flow inject node i did:
msg.payload = global.env.MY_ENV_VAR

That should work - env.get(...) eventually does look up from process.env.

Did you remember to restart Node-RED after editing your settings file?

:slight_smile: yes i did, stopped and restarted the docker...

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