Report import of EnvironmentFile in Node-red console / log

Background

On Linux Node-red can be executed in various ways:
  1. sudo systemctl enable nodered & reboot
  2. sudo systemctl [re]start nodered
  3. node-red-start
  4. node-red-pi
  5. node-red

Edit - corrected (swapped 3 & 4), added 5.
1, 2 and 3 will (always - assuming the OS supports systemd?) start Node-red as a systemd service using the script at /lib/systemd/system/nodered.service
In this case, any environment variables specified in a file ~/.node-red/EnvironmentFile will be inherited by Node-red *

4 and 5 start Node-red directly, not using the systemd script, so the file is not imported.
Note that node-red-start is very widely recommended as the start-up command of choice despite it bypassing this and other advantages of the systemd script file.

* Or it may be ~/.node-red/environment, depending on the installer script version first used on that machine.

Problem

The confusingly varied startup behaviour makes it hard to tell if required environment variables have actually been imported.

Proposal

**1** Add `dd mmm HH:MM:SS - [info] Environment file : /home/pi/.node-red/EnvironmentFile` to the current information in the Node-red log:
22 Jul 15:34:20 - [info] Node-RED version: v4.0.0-beta.4
22 Jul 15:34:20 - [info] Node.js  version: v20.15.1
22 Jul 15:34:20 - [info] Linux 6.6.31+rpt-rpi-v8 arm64 LE
22 Jul 15:34:20 - [info] Loading palette nodes
22 Jul 15:34:22 - [info] Dashboard version 3.6.5 started at /ui
22 Jul 15:34:23 - [info] Settings file  : /home/pi/.node-red/settings.js
22 Jul 15:34:23 - [info] Context store  : 'memoryOnly' [module=memory]
22 Jul 15:34:23 - [info] Context store  : 'file' [module=localfilesystem]
22 Jul 15:34:23 - [info] User directory : /home/pi/.node-red
**************** This line
22 Jul 15:34:23 - [info] Environment file  : /home/pi/.node-red/EnvironmentFile   
****************
22 Jul 15:34:23 - [warn] Projects disabled : editorTheme.projects.enabled=false
22 Jul 15:34:23 - [info] Flows file     : /home/pi/.node-red/flows.json
22 Jul 15:34:23 - [info] Server now running at http://127.0.0.1:1880/

2 Bring the script node-red-start into line with the systemd script.

It is 1, 2 and 4 that start it as a service.
3 (node-red-pi) and 5 (node-red) start it in the current window.

Edit: I don't know what the difference between node-red and node-red-pi is. Calling either with --help yields the same output.

Hmm is that right? I could have sworn I'd checked and shuffled them around to get like methods together.

Just underlines how confusing these different options are.

And I'd forgotten just plain node-redeven though I use that after every reinstallation

Run node-red-start and it tells you it starting as a service.

node-red-start

Start Node-RED
 
Once Node-RED has started, point a browser at http://192.168.49.92:1880
On Pi Node-RED works better with the Firefox or Chrome browser
 
Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot
 
To find more nodes and example flows - go to http://flows.nodered.org
 
Starting as a systemd service.

Yes I just got to my PC and tried it, hopefully corrected the original post.

Are you sure the environment file is not read when it is run not as a service?

Well I'm sure the environment variables are not set, and since it's the systemd script which references the file ...

EnvironmentFile=-/home/pi/.node-red/EnvironmentFile

Here is an example:
EnvironmentFile contains

pi@AlsoPi:~/.node-red $ cat EnvironmentFile
MQTT_BROKER='broker.hivemq.com'
MQTT_TOPIC='topic/from/EnvironmentFile'

This is the flow. Note two different ways to refer to environment variables in the inject node.
image

[{"id":"c0c11435666359ac","type":"inject","z":"654f55124d2a7bb4","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"${MQTT_TOPIC}","payload":"MQTT_BROKER","payloadType":"env","x":130,"y":840,"wires":[["b15b20cf035d36f3"]]},{"id":"b15b20cf035d36f3","type":"debug","z":"654f55124d2a7bb4","name":"From Environment","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":310,"y":840,"wires":[]}]

After sudo systemctl restart node-red the debug output shows the file is imported.
image

After sudo systemctl stop node-red and node-red it shows that the environment variables are not set.
msg.topic gets the variable name as a string literal because this field is injected as a string.
msg.payload is blank because it's injected as type Environment variable.
image

And note that this example is Node-red 4 but the filename in systemd is EnvironmentFile because [an earlier version of] Node-red was previously installed.
If I were to reflash the SD card and install again, I believe the filename would be environment