Global, Flow and Environment Variables
Each flow contains one or multiple chains of nodes that branch, are combined with subflows, or linked to different flows. Managing variables and scopes of access to these variables can have different approaches.
Using global and flow variables
You can add context data with global and flow variables. Those can be created with a Change node, which has to be triggered as part of a flow or separately, with the provided Config node, which will set permanent global values, or as part of a function. Flow and Global variables can be set and fetched with two commands.
flow.set('flowVariable', value)
flow.get('flowVariable')
global.set('globalVariable', value)
global.get('globalVariable')
You can use flow and global variables to set up values that should be accessible throughout the flow they were set in or the whole project.
Adding a configuration node
The context data can be used to create configuration nodes. Configuration nodes can store information that should be available to every node in the flow or project. The advantage of a configuration node is having a dedicated space for input values. Instead of updating hardcoded information in every node in your project, you can set them up to use context variables and edit only the configuration node if the values have to be updated.
Node-RED creates its configuration nodes. Those are automatically created. Users can't create a configuration node outside of the workspace.
You can create the functionality of a configuration node in different ways. You can use every node that alters or creates context data. Both Change and Function nodes let you access context data. You can add them at the start of your flow to set the values every time the flow gets triggered. Alternatively, you can place these nodes into their separate flow or disconnected them from the other nodes. You only have to trigger them to store the values. A simple trigger would be manual with an Inject node.
The Config node, which is preinstalled but not part of the core nodes of Node-RED, provides the exact functionality of a configuration node. You can set global and flow values. The node doesn't have to be triggered. The variables are created and stored immediately after a redeployment. You can set up multiple nodes and switch between them by setting the node you want to use to activate and deactivate the other configuration nodes.
Working with environment variables
Environment variables can be added to subflows. New variables can be added to their Node options. The environment variables are then accessible in every node in the subflow. They can be used as variables in a function node or as JSONata expressions.
//JavaScript
msg.payload = env.get('EnvVar')
//JSONata
$env('EnvVar')
Some nodes, like the Change node, provide the option to set or change the value of environment variables with the env variable option as part of a selection of data types.
Environment variables help to control the information that is available in a subflow. When working with multiple flows and subflows, controlling variables that are accessible at any given point in your project can get complicated. Using environment variables in subflows helps set scopes for values and structure the flow of data through a flow of nodes.