--env-file=file
从相对于当前目录的文件加载环境变量,使它们在 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.】