|
| 1 | +# Environment Variables |
| 2 | + |
| 3 | +Environment variables are variables associated to the environment the Node.js process runs in. |
| 4 | + |
| 5 | +## CLI Environment Variables |
| 6 | + |
| 7 | +There is a set of environment variables that can be defined to customize the behavior of Node.js, for more details refer to the [CLI Environment Variables documentation][]. |
| 8 | + |
| 9 | +## `process.env` |
| 10 | + |
| 11 | +The basic API for interacting with environment variables is `process.env`, it consists of an object with pre-populated user environment variables that can be modified and expanded. |
| 12 | + |
| 13 | +For more details refer to the [`process.env` documentation][]. |
| 14 | + |
| 15 | +## DotEnv |
| 16 | + |
| 17 | +Set of utilities for dealing with additional environment variables defined in `.env` files. |
| 18 | + |
| 19 | +> Stability: 1.1 - Active development |
| 20 | +
|
| 21 | +<!--introduced_in=v20.12.0--> |
| 22 | + |
| 23 | +### .env files |
| 24 | + |
| 25 | +`.env` files (also known as dotenv files) are files that define environment variables, which Node.js applications can then interact with. |
| 26 | + |
| 27 | +The following is an example of the content of a basic `.env` file: |
| 28 | +```text |
| 29 | +MY_VAR_A = "my variable A" |
| 30 | +MY_VAR_B = "my variable B" |
| 31 | +``` |
| 32 | + |
| 33 | +This type of file is used in various different programming languages and platforms but there is no formal specification for it, therefore Node.js defines its own specification described below. |
| 34 | + |
| 35 | +A `.env` file is a file that contains key-value pairs, each pair is represented by a variable name followed by the equal sign (`=`) followed by a variable value. |
| 36 | + |
| 37 | +The name of such files is usually `.env` or it starts with `.env` (like for example `.env.dev` where `dev` indicates a specific target environment). This is the recommended naming scheme but it is not mandatory and dotenv files can have any arbitrary file name. |
| 38 | + |
| 39 | +#### Variable Names |
| 40 | + |
| 41 | +A valid variable name must contain only letters (uppercase or lowercase), digits and underscores (`_`) and it can't begin with a digit. |
| 42 | + |
| 43 | +More specifically a valid variable name must match the following regular expression: |
| 44 | +```js |
| 45 | +/^[a-zA-Z_]+[a-zA-Z0-9_]*$/ |
| 46 | +``` |
| 47 | + |
| 48 | +The recommended convention is to use capital letters with underscores and digits when necessary, but any variable name respecting the above definition will work just fine. |
| 49 | + |
| 50 | +For example, the following are some valid variable names: `MY_VAR`, `MY_VAR_1`, `my_var`, `my_var_1`, `myVar`, `My_Var123`, while these are instead not valid: `1_VAR`, `'my-var'`, `"my var"`, `VAR_#1`. |
| 51 | + |
| 52 | +#### Variable Values |
| 53 | + |
| 54 | +Variable values are comprised by any arbitrary text, which can optionally be wrapped inside single (`'`) or double (`"`) quotes. |
| 55 | + |
| 56 | +Quoted variables can span across multiple lines, while non quoted ones are restricted to a single line. |
| 57 | + |
| 58 | +Note that when parsed by Node.js all values are interpreted as text, meaning that any value will result in a JavaScript string inside Node.js. For example the following values: `0`, `true` and `{ "hello": "world" }` will result in the literal strings `'0'`, `'true'` and `'{ "hello": "world" }'` instead of the number zero, the boolean `true` and an object with the `hello` property respectively. |
| 59 | + |
| 60 | +Examples of valid variables: |
| 61 | +```text |
| 62 | +MY_SIMPLE_VAR = a simple single line variable |
| 63 | +MY_EQUALS_VAR = "this variable contains an = sign!" |
| 64 | +MY_HASH_VAR = 'this variable contains a # symbol!' |
| 65 | +MY_MULTILINE_VAR = ' |
| 66 | +this is a multiline variable containing |
| 67 | +two separate lines\nSorry, I meant three lines' |
| 68 | +``` |
| 69 | + |
| 70 | +#### Spacing |
| 71 | + |
| 72 | +Leading and trailing whitespace characters around variable keys and values are ignored unless they are enclosed within quotes. |
| 73 | + |
| 74 | +For example: |
| 75 | +```text |
| 76 | + MY_VAR_A = my variable a |
| 77 | + MY_VAR_B = ' my variable b ' |
| 78 | +``` |
| 79 | +will be treaded identically to: |
| 80 | +```text |
| 81 | +MY_VAR_A = my variable |
| 82 | +MY_VAR_B = ' my variable b ' |
| 83 | +``` |
| 84 | + |
| 85 | +#### Comments |
| 86 | + |
| 87 | +Hash-tag (`#`) characters denote the beginning of a comment, meaning that the rest of the line will be completely ignored. |
| 88 | + |
| 89 | +Hash-tags found within quotes are however treated as any other standard character. |
| 90 | + |
| 91 | +For example: |
| 92 | +```text |
| 93 | +# This is a comment |
| 94 | +MY_VAR = my variable # This is also a comment |
| 95 | +MY_VAR_A = "# this is NOT a comment" |
| 96 | +``` |
| 97 | + |
| 98 | +#### `export` prefixes |
| 99 | + |
| 100 | +The `export` keyword can optionally be added in front of variable declarations, such keyword will be completely ignored |
| 101 | +by all processing done on the file. |
| 102 | + |
| 103 | +This is useful so that the file can be sourced, without modifications, in shell terminals. |
| 104 | + |
| 105 | +Example: |
| 106 | +```text |
| 107 | +export MY_VAR = my variable |
| 108 | +``` |
| 109 | + |
| 110 | +### CLI Options |
| 111 | + |
| 112 | +`.env` files can be used to populate the `process.env` object via one the following CLI options: |
| 113 | + |
| 114 | +- [`--env-file=file`][] |
| 115 | + |
| 116 | +- [`--env-file-if-exists=file`][] |
| 117 | + |
| 118 | +### Programmatic APIs |
| 119 | + |
| 120 | +There following two functions allow you to directly interact with `.env` files: |
| 121 | + |
| 122 | +- [`process.loadEnvFile`][] loads an `.env` file and populates `process.env` with its variables |
| 123 | + |
| 124 | +- [`util.parseEnv`][] parses the row content of an `.env` file and returns its value in an object |
| 125 | + |
| 126 | +[CLI Environment Variables documentation]: https://nodejs.org/api/cli.html#environment-variables_1 |
| 127 | +[dotenv library]: https://github.com/motdotla/dotenv |
| 128 | +[`process.env` documentation]: process.md#processenv |
| 129 | +[`process.loadEnvFile`]: process.md#processloadenvfilepath |
| 130 | +[`--env-file=file`]: cli.md#--env-filefile |
| 131 | +[`--env-file-if-exists=file`]: cli.md#--env-file-if-existsfile |
| 132 | +[`util.parseEnv`]: util.md#utilparseenvcontent |
| 133 | + |
0 commit comments