Navigate back to the homepage

node-red environment variables example

July 17th, 2022 · 1 min read
node-red environment variables example

Hi Friends 👋,

Welcome To Infinitbility! ❤️

use environment variables in node-red

To use environment variables in node-red,

  1. install the dotenv and path packages
  2. create a .env file with some key and value
  3. load env in your node-red js file
  4. access env variables using process.env

After doing the above steps you can use your env variables using process.env like the below code example

1const path = require('path')
2require('dotenv').config({path: path.resolve(__dirname, './.env')})

Node red environment variables example

Here, we will create custom nodes every step to set up the env configuration in the node-red module.

Want to know the steps to create a node in nodes?

How to create multiple nodes in the node-red single module?

I’m not going to explain step by step process of creating or installing nodes in node-red but if want to know best practices read the above-mentioned article.

Let’s create a node…

I’m assuming you have already created your own node-red node.


To configure env you have to install two packages dotenv and path packages.

Open your terminal, and navigate to your node project directory. and run the below command to install.

1npm install dotenv path

Create env file

create a .env file in your node root directory your package.json file is located.



load env variables in js file

this is the most important and last step to configure env variables in your node-red node module.

put two lines on the top of the js file.

1const path = require('path')
2require('dotenv').config({path: path.resolve(__dirname, './.env')})

Here, your steps are done now you can access your environment variables using process.env.


Here is my node js and HTML file


1const path = require('path')
2require('dotenv').config({path: path.resolve(__dirname, './.env')})
4module.exports = function(RED) {
5 function EnvNode(config) {
6 RED.nodes.createNode(this,config);
7 var node = this;
8 node.on('input', function(msg) {
9 let obj = {apiUrl: process.env.API_URL};
10 msg.payload = obj;
11 node.send(msg);
12 });
13 }
14 RED.nodes.registerType("node-env",EnvNode);


1<script type="text/html" data-template-name="node-env">
4<script type="text/html" data-help-name="node-env">
5 <p>
6 A simple node that converts the message payloads into all node-env
7 characters
8 </p>
11<script type="text/javascript">
12 RED.nodes.registerType("node-env", {
13 category: "Toolkit",
14 color: "#a6bbcf",
15 defaults: {
16 name: { value: "" },
17 },
18 inputs: 1,
19 outputs: 1,
20 icon: "file.png",
21 label: function () {
22 return || "node-env";
23 },
24 });

let’s check it with the node-red flow it should send environment variables in response.

Node-red, environment variables example
Node-red, environment variables example

node-red-environment-variables-example Github Repo

node-red-dropdown-example Github Repo

multi-node-example node-red Github Repo

Create custom node in node-red

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide



More articles from Infinitbility

how to add new line in string in typescript?

how to add new line in string in typescript?

TypeScript, add new line example with step-by-step guide.

July 16th, 2022 · 1 min read
how to add space in string in typescript?

how to add space in string in typescript?

TypeScript, add space in string example with step-by-step guide.

July 16th, 2022 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]