Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16b1f7a

Browse files
dario-piotrowiczrichardlau
authored andcommitted
doc: add new environment variables doc page
add a new doc page for environment variables, this would be theone-stop place for all evnrioment variables informations (oftenreferring to other doc pages)the main purpose of this page would also to provide a standardNode.js specification for `.env` filesPR-URL:#59052Fixes:#58807Reviewed-By: James M Snell <jasnell@gmail.com>
1 parentdafd561 commit16b1f7a

File tree

4 files changed

+163
-6
lines changed

4 files changed

+163
-6
lines changed

‎doc/api/cli.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ node --entry-url 'file.ts?query#hash'
792792
node --entry-url'data:text/javascript,console.log("Hello")'
793793
```
794794

795-
###`--env-file-if-exists=config`
795+
###`--env-file-if-exists=file`
796796

797797
<!-- YAML
798798
added: v22.9.0
@@ -803,7 +803,7 @@ added: v22.9.0
803803
Behavior is the same as[`--env-file`][], but an error is not thrown if the file
804804
does not exist.
805805

806-
###`--env-file=config`
806+
###`--env-file=file`
807807

808808
<!-- YAML
809809
added: v20.6.0
@@ -3922,8 +3922,8 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
39223922
[`--cpu-prof-dir`]:#--cpu-prof-dir
39233923
[`--diagnostic-dir`]:#--diagnostic-dirdirectory
39243924
[`--disable-sigusr1`]:#--disable-sigusr1
3925-
[`--env-file-if-exists`]:#--env-file-if-existsconfig
3926-
[`--env-file`]:#--env-fileconfig
3925+
[`--env-file-if-exists`]:#--env-file-if-existsfile
3926+
[`--env-file`]:#--env-filefile
39273927
[`--experimental-default-type=module`]:#--experimental-default-typetype
39283928
[`--experimental-sea-config`]:single-executable-applications.md#generating-single-executable-preparation-blobs
39293929
[`--heap-prof-dir`]:#--heap-prof-dir

‎doc/api/environment_variables.md‎

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

‎doc/api/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*[Diagnostics Channel](diagnostics_channel.md)
2828
*[DNS](dns.md)
2929
*[Domain](domain.md)
30+
*[Environment Variables](environment_variables.md)
3031
*[Errors](errors.md)
3132
*[Events](events.md)
3233
*[File system](fs.md)

‎doc/contributing/advocacy-ambassador-program.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ with projects learning from one another and their users.
268268
|[testing source code](https://nodejs.org/api/test.html)|[16.17.0](https://nodejs.org/en/blog/release/v16.17.0)| Stable as of 20.0.0|
269269
|[watching source code](https://nodejs.org/api/cli.html#--watch)|[16.19.0](https://nodejs.org/en/blog/release/v16.19.0)| Stable as of 20.13.0|
270270
|[parsing arguments](https://nodejs.org/api/util.html#utilparseargsconfig)|[18.3.0](https://nodejs.org/en/blog/release/v18.3.0)| Stable as of 20.0.0|
271-
|[reading environment](https://nodejs.org/api/cli.html#--env-fileconfig)|[20.6.0](https://nodejs.org/en/blog/release/v20.6.0)| Active Development|
271+
|[reading environment](https://nodejs.org/api/cli.html#--env-filefile)|[20.6.0](https://nodejs.org/en/blog/release/v20.6.0)| Active Development|
272272
|[styling output](https://nodejs.org/docs/latest-v22.x/api/util.html#utilstyletextformat-text-options)|[20.12.0](https://nodejs.org/en/blog/release/v20.12.0)| Stable, as of[22.13.0](https://github.com/nodejs/node/pull/56329)|
273273
|[run scripts](https://nodejs.org/docs/latest/api/cli.html#--run)|[22.0.0](https://nodejs.org/en/blog/release/v22.0.0)| Stable, as of 22.0.0|
274274
|[run TypeScript](https://nodejs.org/api/cli.html#--experimental-strip-types)|[22.6.0](https://nodejs.org/en/blog/release/v22.6.0)| Active Development|
@@ -281,7 +281,7 @@ with projects learning from one another and their users.
281281
*<https://nodejs.org/api/test.html>
282282
*<https://nodejs.org/api/cli.html#--watch>
283283
*<https://nodejs.org/api/util.html#utilparseargsconfig>
284-
*<https://nodejs.org/api/cli.html#--env-fileconfig>
284+
*<https://nodejs.org/api/cli.html#--env-filefile>
285285
*<https://nodejs.org/docs/latest-v22.x/api/util.html#utilstyletextformat-text-options>
286286
*<https://nodejs.org/api/cli.html#--run>
287287
*<https://nodejs.org/api/cli.html#--experimental-strip-types>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp