--env-file=config


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active development

从相对于当前目录的文件加载环境变量,使它们可供 process.env 上的应用使用。解析并应用 配置 Node.js 的环境变量,例如 NODE_OPTIONS。如果在环境和文件中定义了相同的变量,则环境中的值优先。

¥Loads environment variables from a file relative to the current directory, making them available to applications on process.env. The environment variables which configure Node.js, such as NODE_OPTIONS, are parsed and applied. If the same variable is defined in the environment and in the file, the value from the environment takes precedence.

你可以传递多个 --env-file 参数。后续文件将覆盖先前文件中定义的预先存在的变量。

¥You can pass multiple --env-file arguments. Subsequent files override pre-existing variables defined in previous files.

如果文件不存在,则会引发错误。

¥An error is thrown if the file does not exist.

node --env-file=.env --env-file=.development.env index.js 

文件的格式应为每个键值对一行,由 = 分隔的环境变量名称和值:

¥The format of the file should be one line per key-value pair of environment variable name and value separated by =:

PORT=3000 

# 之后的任何文本都被视为注释:

¥Any text after a # is treated as a comment:

# This is a comment
PORT=3000 # This is also a comment 

值可以以以下引号开始和结束:```、"'。它们从值中被省略。

¥Values can start and end with the following quotes: `, " or '. They are omitted from the values.

USERNAME="nodejs" # will result in `nodejs` as the value. 

支持多行值:

¥Multi-line values are supported:

MULTI_LINE="THIS IS
A MULTILINE"
# will result in `THIS IS\nA MULTILINE` as the value. 

在忽略某个键之前导出关键字:

¥Export keyword before a key is ignored:

export USERNAME="nodejs" # will result in `nodejs` as the value. 

如果要从可能不存在的文件加载环境变量,可以改用 --env-file-if-exists 标志。

¥If you want to load environment variables from a file that may not exist, you can use the --env-file-if-exists flag instead.