"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Environment Variables: a very short intro for JS development

Environment Variables: a very short intro for JS development

Published on 2024-10-31
Browse:230

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
  • 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
Release Statement This article is reproduced at: https://dev.to/charlesloder/environment-variables-a-very-short-intro-for-js-development-18j3?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3