This package provides a dedicated Node-RED node (`process-env`) for retrieving environment variables from the server process where Node-RED is executing. It serves as a fundamental utility for integrating Node-RED flows with their host environment, enabling external configuration without modifying the flow directly. The node, currently at stable version 1.0.2, simplifies accessing `process.env` values compared to using a generic Function node. Its release cadence is typically driven by bug fixes or minor compatibility updates. A key differentiator is its straightforward configuration within the Node-RED editor, allowing users to specify the environment variable key directly or infer it from incoming message properties (`payload` or `topic`), with a clear order of precedence. This makes it a go-to solution for parameterizing Node-RED applications.
npm install node-red-contrib-envVerified import paths — ran on the pinned version, not inferred.
This Node-RED flow demonstrates reading an environment variable. First, ensure the environment variable `MY_ENV_VAR` is set in the Node-RED server's environment (e.g., `export MY_ENV_VAR='Hello from Env!'` before starting Node-RED). The Inject node triggers the flow. The 'Read MY_ENV_VAR' node (`process-env` type) is configured to look for a variable named `MY_ENV_VAR`. The Debug node displays the retrieved value (or `undefined` if not found) in the Node-RED debug sidebar.
Restart the Node-RED instance after modifying environment variables to ensure the changes are picked up by the runtime. For Docker, this means restarting the container.
Clearly define a single source for the environment variable key to avoid ambiguity. If you intend to use `msg.payload` or `msg.topic`, ensure the node's `key` property is left blank. Alternatively, use a preceding Change node to normalize the incoming message if necessary.
If dynamic access to environment variables within a flow is required, consider using a Function node with `process.env.MY_VAR`, or Node-RED's built-in `$env()` function in Change nodes or `env.get()` in Function nodes, which are also evaluated at runtime. For configuration that truly needs to change at runtime *without* a Node-RED restart, consider using flow or global context, or external databases/APIs, rather than environment variables.
Ensure Node-RED is running in a standard Node.js environment where `process.env` is accessible. This error is rare for this specific node unless the Node-RED installation itself is corrupted or running in an unusual sandbox.
Verify that the environment variable is correctly set in the operating system or environment where Node-RED is launched (e.g., `export MY_VAR=value` in shell, `Environment=` in `systemd`, or defined in Docker Compose). Double-check for typos in the variable name within the node's configuration. Use a Debug node before the `process-env` node to ensure `msg.payload` or `msg.topic` contains the expected variable name if those inputs are being used.