DEV Community

Charles Loder
Charles Loder

Posted on

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 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


Enter fullscreen mode Exit fullscreen mode
  • 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


Enter fullscreen mode Exit fullscreen mode

(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"


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)