Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs Environment variables vs config file

Actually I have a nodejs express app with its config file for params like host, port, JWT token, DB params and more.

The question is if it could have sense to keep those params directly on environment variables (whitout any config file) and acces them without the need of do the "require" for config in all components and modules.

All examples I see uses a config file, probably something about security or memory?

like image 823
CVO Avatar asked Jan 25 '19 10:01

CVO


People also ask

Do we need to set environment variables for node JS?

You really do not need to set up your own environment to start learning Node. js. Reason is very simple, we already have set up Node.

What is config file in node JS?

Node-config allows you to create configuration files in your Node application for different deployment environments. With it, you can define a default configuration file that you intend to repeat across environments, then extend the default config to other environments, such as development, staging, etc.

Is .env a config file?

In case you are still wondering what all this means, well, you are probably new to the . env file. It's actually a simple configuration text file that is used to define some variables you want to pass into your application's environment. This file needs a something like a parser to make it work.

What is .env file in node JS?

The dotenv package for handling environment variables is the most popular option in the Node. js community. You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.

What is node config in Node JS?

Node-config allows you to create configuration files in your Node application for different deployment environments. With it, you can define a default configuration file that you intend to repeat across environments, then extend the default config to other environments, such as development, staging, etc.

How to load environment variables in Node JS?

Load a .env file in the Node.js project. First, you have to create a file with the name .env in the root of your project directory. Inside this file, you can write all of your environment variables. One in each line. Each variable will have 2 parts separated by an equal (=) symbol. The left side is the key and the right side is the value.

What happens if environment variables are not set in node config?

If your environment variables are not set, the services that use them will break. So, you should ensure your environment variables are tested. Node-config provides multiple utilities, one of which is the config.has() method that allows you to verify that environment variables are set.

What is an ENV file in Node JS?

In Node.js, .env is a file that is used to declare multiple environment variables. Normally, this file will stay at the root of a project. It looks like a text document but only contains key-value pairs. It is possible to write a single-line comment using the hash (#) symbol. Why should you use a .env file?


2 Answers

config file is usually for setting the default values for your environment variables,

which is needed when you are writing the test cases and need to use default values or mock values,

and also you will have all the env variables at one place which is better management.

so if you have an environment variable x,

in config file you can keep it as

config.x = process.env.x || 'defaultVale or mockValue'

like image 151
AZ_ Avatar answered Nov 05 '22 19:11

AZ_


A config file lets your very quickly set the entire environment of a machine - eg S3 buckets, API urls, access keys, etc. If you separate these into separate process.env.VARIABLE then you would need to set each of these...for which you would likely make a script...and now you have an environment file again!

To access environment variables you can use process.env.VARIABLE in your nodejs code (is always a string), as long as the variable is set before the process is started.

like image 43
Tobin Avatar answered Nov 05 '22 21:11

Tobin