Environment Variables: a very short intro for JS development

This article is intended to be a short invesitgation into environment variables for myself, hence the terse style

The basics

The terminal is an app that is really a terminal emulator

We use the terminal interact with the shell

The shell …


This content originally appeared on DEV Community and was authored by Charles Loder

This article is intended to be a short invesitgation into environment variables for myself, hence the terse style

The basics

  • The terminal is an app that is really a terminal emulator
  • We use the terminal interact with the shell
  • The shell can spawn processes, such as a Javascript program by means of another app, like node
    • e.g. shell -> node -> index.js
  • When we open terminal a new session is created in the shell
  • This session has variables that can be associated with it (e.g. echo $USER prints the name of the current user)

node

In node, environment variables are accessed via the global process.env

console.log(process.env.USER); // username
  • Applications typically access secrets or configuration related data via environment variables
    • e.g. process.env.DB_CONNECTION
    • This is helpful because we want to keep secrets secret, and not every user will have the same configuration needs — this allows for better decoupling of logic and configuration

methods for loading variables

There are a few ways we can make these variables available to our programs.

(1) making them available via the command line:

Pretty straightforward — just call the command with the variable in the command line.

DB_CONNECTION="postgresql://username:password@host:port/database_name" node index.js

(2) storing them in an .env file

.env files consist of KEYS and VALUES which are separated by an equals sign.

DB_CONNECTION="postgresql://username:password@host:port/database_name"

These files are commmon, and there are a few ways to make them available to your application.

direnv

Use a tool like direnv which loads variables from a file makes them available in the shell; it is this is typically installed globally.

By default direnv looks for a .envrc file, but it can use .env as well, see here for configuring it to do so.

dotenv

Use a tool like dotenv which loads variables from a file and makes them available in process.env.

direnv vs dotenv

There are pros and cons for each method.

Using direnv is language agnostic and means one less dependency, but it also means that consumers of your application need their own way to load environment variables if not using direnv;

Using dotenv ensures that consumers of the application can just use a .env file with no worries, but it does add a package just to do something the shell can do natively.

node flag

Node recently included support (v20.6.0) for using .env files directly.

A flag has to be used

node --env-file=.env index.js

Like dotenv, this makes the content of .env available in process.env.

Summary

  • environment variables are technology native to the shell
  • there are a few different ways to load them — directly, direnv, dotenv, node
  • direnv loads variables into your shell, but dotenv and node only make them available to your application via process.env
  • if using node, you should probably opt to use the new --env-file flag


This content originally appeared on DEV Community and was authored by Charles Loder


Print Share Comment Cite Upload Translate Updates
APA

Charles Loder | Sciencx (2024-09-06T01:04:34+00:00) Environment Variables: a very short intro for JS development. Retrieved from https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/

MLA
" » Environment Variables: a very short intro for JS development." Charles Loder | Sciencx - Friday September 6, 2024, https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/
HARVARD
Charles Loder | Sciencx Friday September 6, 2024 » Environment Variables: a very short intro for JS development., viewed ,<https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/>
VANCOUVER
Charles Loder | Sciencx - » Environment Variables: a very short intro for JS development. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/
CHICAGO
" » Environment Variables: a very short intro for JS development." Charles Loder | Sciencx - Accessed . https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/
IEEE
" » Environment Variables: a very short intro for JS development." Charles Loder | Sciencx [Online]. Available: https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/. [Accessed: ]
rf:citation
» Environment Variables: a very short intro for JS development | Charles Loder | Sciencx | https://www.scien.cx/2024/09/06/environment-variables-a-very-short-intro-for-js-development/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.