# Include Env Variables configuration to flows.json

**URL:** <https://discourse.nodered.org/t/include-env-variables-configuration-to-flows-json/93739>\
**Category:** Feature Requests\
**Created:** [6 December 2024 18:18 UTC](https://discourse.nodered.org/t/include-env-variables-configuration-to-flows-json/93739 "2024-12-06T18:18:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AllanOricil](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allanoricil/32/106911_2.png) [@AllanOricil](https://discourse.nodered.org/u/AllanOricil)\
**Post date:** [6 December 2024 18:18 UTC](https://discourse.nodered.org/t/include-env-variables-configuration-to-flows-json/93739/1 "2024-12-06T18:18:38Z")

</div>

**obs: this feature is not asking to serialize env variable values!**

I noticed that every time when I import a `flows.json`, in order to avoid late runtime issues, I must search for `$env` (jsonata) or `env.get` (function node) just to determine which environment variables my flow needs. After that, I also need to check which ones are actually mandatory, since there are situations where a fallback value is used when a particular env variable is not set. To mitigate this issue, developers can write a document describing all env variables their flow needs, however this solution won't solve runtime issues due to missing env variables because people can still forget to set some values. To solve this other issue, developers can add a function node that throws an exception if any of the env variables are missing, however they will only see this error if a message is processed by this node, which may not even happen, or happen too late.

So, since I could not find a solution in the editor that would help me to determine which env variables my flows require, my suggestion is to include this information in the exported `flows.json`, in a section where the developer, editor and runtime, can easily determine which environment variables are necessary, similarly as this [feature](https://github.com/node-red/node-red/pull/4599), which includes the dependencies a flow requires. For example, in a exported `flow.json` there would exist a new object describing the environment variables

```json
[
  {
    "id": "5bb3c78cfb5d197f"
    "type": "global-config",
    "modules": {...},
    "environment_variables": [
        {
            "key" : "MY_ENV",
            "value": "fallback value", // THIS IS NOT SERIALIZING THE VALUE. IT IS A FALLBACK ONE WHICH IS ALWAYS OVERWRITTEN BY THE CONFIGURED VALUE
            "description": "this environment variable is used to...",
            "required": false // the editor and runtime won't require this one to be added before deploying the flow
        },
        {
            "key" : "MY_REQUIRED_ENV",
            "description": "this environment variable is used to...",
            "required": true // the editor and runtime will require this one to be added before deploying the flow
        },
    ]
  }
]

```

### editor error

When importing a flow that requires env variables, the Editor will notify the User about it, and tell him/her which ones must be set in his environment.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/012c0e2a2763601b8d302d94d8c7c542b81c40e3.png)

This will remove runtime errors due to missing environment variables.

### runtime error

A runtime error is also thrown when a flow configured with required env variables can't find values in the environment.

When the runtime finds all environment values in the environment, the flow starts

```bash
...
Checking environment variables 
Loading nodes
Loading plugins
Flows started
...

```

When the runtime can't find at least 1 required environment variable value, an exception is raised and the flow won't start. It is similar as when a dependency is missing.

```bash
Checking environment variables
Error: the following env variable MY_REQUIRED_ENV_VAR is not set. Please, set it in the environment before running this flow.

```

When a flow has an environment variable reference that ins't listed in the global config, a different error will be raised

```bash
Checking environment variables
Error: a reference to an environment variable called MY_UNCONFIGURABLE_ENV_VAR has been found at {node_id}. Please, configure this env variable in the global config.

```

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [6 December 2024 19:31 UTC](https://discourse.nodered.org/t/include-env-variables-configuration-to-flows-json/93739/2 "2024-12-06T19:31:25Z")

</div>

Mmm honestly I don't know how the editor could allow the user to easily import env variables. Something like the conflict window.

I think extending what Subflow does to groups and tabs would help. As a first step to define env metadata.

> <https://github.com/node-red/node-red/issues/4890>
>
> As discussed on the forum: https://discourse.nodered.org/t/environment-variables…-support-config-node-type-on-groups-and-tabs/91219
> 
> Starting with Node-RED 4, \*config nodes\* can be configured as subflow parameters in its environment list.
> 
> This feature could be extended to the environment variable lists of \*\*editor groups\*\* and \*\*tabs\*\*, and if possible even the global environment located in the settings dialog.
> 
> \#### Advantages
> 
> \* set a config node for a group of nodes in one place
> \* change the config node without having to open/edit every single node
> \* when moving nodes to another group/tab,, it can automatically use the config node from the local environment over there, without changing the node's config
> 
> \#### Example use case
> 
> In combination with Dashboard nodes, it can ease the configuration effort of single widgets a lot. You could simply group them in the editor and set the ui-group node in a single place.
> 
> As a generic feature, I'm certain this can be useful for other nodes with config nodes as well (mqtt broker, database config, ...).

Then have to handle the import/export of this data.

A lot of work 🤯

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 February 2025 19:31 UTC](https://discourse.nodered.org/t/include-env-variables-configuration-to-flows-json/93739/3 "2025-02-04T19:31:49Z")

</div>

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