Navigate back to the homepage

node-red environment variables example

Infinitbility
Node-red
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')})
3
4process.env.API_URL;

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.

Installtion

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.

.env

1API_URL=https://infinitbility.com

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.

1process.env.API_URL

Here is my node js and HTML file

node-red-environment-variables-example/index.js

1const path = require('path')
2require('dotenv').config({path: path.resolve(__dirname, './.env')})
3
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);
15}

node-red-environment-variables-example/index.html

1<script type="text/html" data-template-name="node-env">
2</script>
3
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>
9</script>
10
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 this.name || "node-env";
23 },
24 });
25</script>

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

Tutorials

Categories

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
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]