跳到内容

如何从 Node.js 读取环境变量

🌐 How to read environment variables from Node.js

Node.js 的 process 核心模块提供了 env 属性,它包含了在进程启动时设置的所有环境变量。

🌐 The process core module of Node.js provides the env property which hosts all the environment variables that were set at the moment the process was started.

以下代码运行 app.js 并设置 USER_IDUSER_KEY

🌐 The below code runs app.js and set USER_ID and USER_KEY.

USER_ID=239482 USER_KEY=foobar node app.js

这将把用户 USER_ID 传递为 239482,并将 USER_KEY 传递为 foobar。这适用于测试,但在生产环境中,你可能需要配置一些 bash 脚本来导出变量。

🌐 That will pass the user USER_ID as 239482 and the USER_KEY as foobar. This is suitable for testing, however for production, you will probably be configuring some bash scripts to export variables.

注意:process 不需要导入,它是 Node.js 中的全局对象。

这里有一个示例,它访问了我们在上面的代码中设置的 USER_IDUSER_KEY 环境变量。

🌐 Here is an example that accesses the USER_ID and USER_KEY environment variables, which we set in above code.

.(..); // "239482"
.(..); // "foobar"

以同样的方式,你可以访问你设置的任何自定义环境变量。

🌐 In the same way you can access any custom environment variable you set.

Node.js 20 引入了对 .env 文件 的【实验性】支持(链接)。

🌐 Node.js 20 introduced experimental support for .env files.

现在,你可以使用 --env-file 标志在运行 Node.js 应用时指定一个环境文件。下面是一个示例 .env 文件以及如何使用 process.env 访问其中的变量。

🌐 Now, you can use the --env-file flag to specify an environment file when running your Node.js application. Here's an example .env file and how to access its variables using process.env.

# .env file
PORT=3000

在你的 js 文件中

🌐 In your js file

.(..); // "3000"

在设置了 .env 文件中的环境变量后运行 app.js 文件。

🌐 Run app.js file with environment variables set in .env file.

node --env-file=.env app.js

此命令会从 .env 文件加载所有环境变量,使其在 process.env 上可用于应用

🌐 This command loads all the environment variables from the .env file, making them available to the application on process.env

此外,你可以传递多个 --env-file 参数。后续的文件会覆盖之前文件中定义的已有变量。

🌐 Also, you can pass multiple --env-file arguments. Subsequent files override pre-existing variables defined in previous files.

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

注意:如果同一个变量在环境和文件中都被定义,则环境中的值优先。

如果你想可选地从 .env 文件读取,可以使用 --env-file-if-exists 标志,以避免在文件丢失时抛出错误。

🌐 In case you want to optionally read from a .env file, it's possible to avoid throwing an error if the file is missing using the --env-file-if-exists flag.

node --env-file-if-exists=.env app.js

使用 process.loadEnvFile(path) 以编程方式加载 .env 文件

🌐 Loading .env files programmatically with process.loadEnvFile(path)

Node.js 提供了一个内置 API,可以直接从代码中加载 .env 文件:process.loadEnvFile(path)

🌐 Node.js provides a built-in API to load .env files directly from your code: process.loadEnvFile(path).

此方法将变量从 .env 文件加载到 process.env 中,类似于 --env-file 标志的作用 - 但可以通过编程方式调用。

🌐 This method loads variables from a .env file into process.env, similar to how the --env-file flag works — but can be invoked programmatically.

由于该方法是在初始化后调用的,启动相关的环境变量(即 NODE_OPTIONS)的设置对进程没有影响(但是,这些变量仍然可以通过 process.env 访问)。

🌐 Because this method is invoked post-initialization, the setting of startup-related environment variables (i.e. NODE_OPTIONS) has no effect on the process (however, these variables can still be accessed via process.env).

示例

🌐 Example

// .env file
PORT=1234

你还可以指定自定义路径:

🌐 You can also specify a custom path:

const {  } = ('node:process');
('./config/.env');