Home > Web Front-end > JS Tutorial > body text

Environment Variables: a very short intro for JS development

WBOY
Release: 2024-09-06 11:01:30
Original
984 people have browsed it

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
Copy after login
  • 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
Copy after login

(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"
Copy after login

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
Copy after login

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

The above is the detailed content of Environment Variables: a very short intro for JS development. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!