Environment variables only strings?

I've set up a file external to my settings.js to store device specific configuration data as discussed in this thread Environment variables outside settings.js file - General - Node-RED Forum (nodered.org)

in settings.js:

var	env_external = require("./device_settings");

device_settings.js

module.exports =  {
	relay_boards: process.env.RELAY_BOARDS = [0, 2, 4],  	// array of relay board ids
}

Are all environment variables stored as a string?

When I do this in a function:

global.set("relay_boards",env.get("RELAY_BOARDS")); // array of id's of boards

Then the global variable is a string "0, 2, 4" when I want it to be an array [0, 2, 4];

The process.env object forces all properties to type string because environment variables must always be strings.


If it just your intention to set a global variable it would be far easier to simply read the file in your flow using a file node.

Store the settings in a JSON file then use a file node then a JSON node (to convert the JSON to an object) then get the values from msg.payload

device_settings.json

{
   "relay_boards": [0, 2, 4],
   "another_setting": 123,
   "one_more": "hello from device_settings.json"
}

function

global.set("relay_boards",msg.payload.relay_boards);



alternatively, if you want to pursue env vars, you could recreate the array in the function...

global.set("relay_boards",env.get("RELAY_BOARDS").split(","));

Thanks for the quick advice Steve! That solution should work perfectly!

Yes, at the and I even used a json file. On a js I could help you more

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