{ "type": "module", "source": "doc/api/environment_variables.md", "modules": [ { "textRaw": "Environment Variables", "name": "environment_variables", "introduced_in": "v20.12.0", "type": "module", "desc": "

Environment variables are variables associated to the environment the Node.js process runs in.

", "modules": [ { "textRaw": "CLI Environment Variables", "name": "cli_environment_variables", "type": "module", "desc": "

There is a set of environment variables that can be defined to customize the behavior of Node.js,\nfor more details refer to the CLI Environment Variables documentation.

", "displayName": "CLI Environment Variables" }, { "textRaw": "DotEnv", "name": "dotenv", "type": "module", "stability": 2, "stabilityText": "Stable", "desc": "

Set of utilities for dealing with additional environment variables defined in .env files.

", "modules": [ { "textRaw": ".env files", "name": ".env_files", "type": "module", "desc": "

.env files (also known as dotenv files) are files that define environment variables,\nwhich Node.js applications can then interact with (popularized by the dotenv package).

\n

The following is an example of the content of a basic .env file:

\n

MY_VAR_A = \"my variable A\"\nMY_VAR_B = \"my variable B\"\n

\n

This type of file is used in various different programming languages and platforms but there\nis no formal specification for it, therefore Node.js defines its own specification described below.

\n

A .env file is a file that contains key-value pairs, each pair is represented by a variable name\nfollowed by the equal sign (=) followed by a variable value.

\n

The name of such files is usually .env or it starts with .env (like for example .env.dev where\ndev indicates a specific target environment). This is the recommended naming scheme but it is not\nmandatory and dotenv files can have any arbitrary file name.

", "modules": [ { "textRaw": "Variable Names", "name": "variable_names", "type": "module", "desc": "

A valid variable name must contain only letters (uppercase or lowercase), digits and underscores\n(_) and it can't begin with a digit.

\n

More specifically a valid variable name must match the following regular expression:

\n

^[a-zA-Z_]+[a-zA-Z0-9_]*$\n

\n

The recommended convention is to use capital letters with underscores and digits when necessary,\nbut any variable name respecting the above definition will work just fine.

\n

For example, the following are some valid variable names: MY_VAR, MY_VAR_1, my_var, my_var_1,\nmyVar, My_Var123, while these are instead not valid: 1_VAR, 'my-var', \"my var\", VAR_#1.

", "displayName": "Variable Names" }, { "textRaw": "Variable Values", "name": "variable_values", "type": "module", "desc": "

Variable values are comprised by any arbitrary text, which can optionally be wrapped inside\nsingle (') or double (\") quotes.

\n

Quoted variables can span across multiple lines, while non quoted ones are restricted to a single line.

\n

Noting that when parsed by Node.js all values are interpreted as text, meaning that any value will\nresult in a JavaScript string inside Node.js. For example the following values: 0, true and\n{ \"hello\": \"world\" } will result in the literal strings '0', 'true' and '{ \"hello\": \"world\" }'\ninstead of the number zero, the boolean true and an object with the hello property respectively.

\n

Examples of valid variables:

\n

MY_SIMPLE_VAR = a simple single line variable\nMY_EQUALS_VAR = \"this variable contains an = sign!\"\nMY_HASH_VAR = 'this variable contains a # symbol!'\nMY_MULTILINE_VAR = '\nthis is a multiline variable containing\ntwo separate lines\\nSorry, I meant three lines'\n

", "displayName": "Variable Values" }, { "textRaw": "Spacing", "name": "spacing", "type": "module", "desc": "

Leading and trailing whitespace characters around variable keys and values are ignored unless they\nare enclosed within quotes.

\n

For example:

\n

   MY_VAR_A   =    my variable a\n    MY_VAR_B   =    '   my variable b   '\n

\n

will be treated identically to:

\n

MY_VAR_A = my variable a\nMY_VAR_B = '   my variable b   '\n

", "displayName": "Spacing" }, { "textRaw": "Comments", "name": "comments", "type": "module", "desc": "

Hash-tag (#) characters denote the beginning of a comment, meaning that the rest of the line\nwill be completely ignored.

\n

Hash-tags found within quotes are however treated as any other standard character.

\n

For example:

\n

# This is a comment\nMY_VAR = my variable # This is also a comment\nMY_VAR_A = \"# this is NOT a comment\"\n

", "displayName": "Comments" }, { "textRaw": "`export` prefixes", "name": "`export`_prefixes", "type": "module", "desc": "

The export keyword can optionally be added in front of variable declarations, such keyword will be completely ignored\nby all processing done on the file.

\n

This is useful so that the file can be sourced, without modifications, in shell terminals.

\n

Example:

\n

export MY_VAR = my variable\n

", "displayName": "`export` prefixes" } ], "displayName": ".env files" }, { "textRaw": "CLI Options", "name": "cli_options", "type": "module", "desc": "

.env files can be used to populate the process.env object via one the following CLI options:

\n

", "displayName": "CLI Options" }, { "textRaw": "Programmatic APIs", "name": "programmatic_apis", "type": "module", "desc": "

There following two functions allow you to directly interact with .env files:

\n

", "displayName": "Programmatic APIs" } ], "displayName": "DotEnv" } ], "properties": [ { "textRaw": "`process.env`", "name": "env", "type": "property", "desc": "

The basic API for interacting with environment variables is process.env, it consists of an object\nwith pre-populated user environment variables that can be modified and expanded.

\n

For more details refer to the process.env documentation.

" } ], "displayName": "Environment Variables" } ] }