Any node property can be set with an environment variable by setting its value
to a string of the form ${ENV_VAR}
. When the runtime loads the flows, it will
substitute the value of that environment variable before passing it to the node.
This only works if it replaces the entire property - it cannot be used to substitute
just part of the value. For example, it is not possible to use CLIENT-${HOST}
.
As nodes provide their own edit dialog, not all properties will provide a text input that can be used to enter the env-var string. In that case, you may consider hand-editing the flow file to set the property.
Within the editor, the TypedInput widget can offer ‘environment variable’ as a type. When this type is selected, its value will be evaluated as follows:
if there is no ${}
present, uses the whole value as the name of the environment
variable.
For example, "FOO"
will be replaced with the value of process.env.FOO
if ${}
is present, it will substitute the corresponding environment
variable into the result:
For example, given the value "Hello ${FOO}"
and the env var FOO
is set to World
,
this results in the value "Hello World"
Environment variables can be accessed in JSONata expressions, such as in the Change
node, using the $env
function:
$env('ENV_VAR')
Within a Function node, environment variables get be accessed using the env.get
function:
let foo = env.get("FOO");
Since Node-RED 3.0
The template
node can access environment variables using the syntax:
My favourite colour is {{env.COLOUR}}.
Since 0.20, Subflows can be configured with instance properties. These appear as environment variables within the Subflow and can be customised for individual instances of the subflow.
For example, given a REST API that provides access to different types of record, a subflow could be created to access the API and handle the response, using an environment variable to identify which type of record should be accessed. Individual instances of the Subflow can then be customised for those particular types.
Since Node-RED 2.1
Environment variables can be set at the flow or group level. This is done in the appropriate tab in the edit dialog for the flow or group.
Since Node-RED 3.1
Environment variables can be set at a global level within the editor. This is done in the User Settings dialog.
When accessing an environment variable in a subflow, Node-RED will search the subflow properties, then the flow containing the subflow (which could be a subflow itself).
In some cases it is useful to access the ‘parent’ levels environment variables
without reference the ‘local’ value. This can be achieved by prefixing the
variable name with $parent.
.
When Node-RED is running as a service having been installed using the provided script, it will not have access to environment variables that are defined only in the calling process. In this instance, environment variables can be defined in the settings file by adding:
process.env.FOO='World';
placed outside the module.exports
section. Alternatively, variables can be defined as part of the systemd
service by placing statements of the form
ENV_VAR='foobar'
in a file named environment
within the Node-RED user directory, ~/.node-red
.
Since Node-RED 2.2
Node-RED defines a set of environment variables for exposing information about the nodes, flows and groups.
This information helps “locate” the node in your workspace. Nodes in your workspace exist as part of a flow. Likewise, a node may (or may not) be part of a group. Nodes, flows and groups are each given unique IDs that are generated by Node-RED.
Nodes, flows and groups all support the name
property, which you can change when editing properties.
The following environment variables can be used to access this information for a given node:
NR_NODE_ID
- the ID of the nodeNR_NODE_NAME
- the Name of the nodeNR_NODE_PATH
- the Path of the node. This represents a node’s position in a flow. It is /
delimited IDs of the flow, enclosing subflows, and the node.NR_GROUP_ID
- the ID of the containing groupNR_GROUP_NAME
- the Name of the containing groupNR_FLOW_ID
- the ID of the flow the node is onNR_FLOW_NAME
- the Name of the flow the node is onNR_SUBFLOW_NAME
- the Name of the containing subflow instance node (since Node-RED 3.1)NR_SUBFLOW_ID
- the ID of the containing subflow instance node (since Node-RED 3.1)NR_SUBFLOW_PATH
- the Path of the containing subflow instance node (since Node-RED 3.1)Note that while the IDs generated by Node-RED are guaranteed to be unique, the names are not. If a node, flow or group does not have a given name, the corresponding environment variable will be an empty string. If a node is not part of a group, its group id environment variable will also return an empty string.
Node-RED: Low-code programming for event-driven applications.
Copyright OpenJS Foundation and Node-RED contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
The OpenJS Foundation | Terms of Use | Privacy Policy | OpenJS Foundation Bylaws | Trademark Policy | Trademark List | Cookie Policy