了解如何在你的项目中使用环境变量。
Expo Router Web 项目可能包括客户端和服务器端环境变量。当你运行 npx expo export
时,客户端环境变量嵌入在应用中并内联在 JavaScript 包中。服务器端环境变量安全地保存在服务器上,并在你运行 eas deploy
时与 API 路由代码一起部署。
¥An Expo Router web project may include client-side and server-side environment variables. The client-side environment variables are embedded in the app and inlined in the JavaScript bundle when you run npx expo export
. The server-side environment variables are kept securely on the server and are deployed with the API routes code when you run eas deploy
.
对于 EAS 环境变量,只能使用纯文本和敏感的 环境变量。当你将提交推送到匹配的分支和/或标签时运行你的工作流程。
¥Client-side environment variables
在浏览器中运行的所有代码都是客户端的。在 Expo Router 项目中,这包括所有非 API Route 或服务器函数的代码。
¥All the code that runs in the browser is client-side. In an Expo Router project, this includes all code that is not an API Route or server function.
客户端代码中的环境变量在构建时内联。你永远不应在客户端代码中放置任何敏感信息,这就是为什么所有客户端环境变量都必须以 EXPO_PUBLIC_
为前缀的原因。
¥The environment variables in your client-side code are inlined at build time. You should never put any sensitive information in your client-side code, which is why all client-side environment variables must be prefixed with EXPO_PUBLIC_
.
然后,当你运行 npx expo export
时,process.env.EXPO_PUBLIC_*
环境变量的所有实例都将被环境中的值替换。
¥Then, when you run npx expo export
, all instances of process.env.EXPO_PUBLIC_*
environment variables will be replaced with values from the environment.
¥Server-side environment variables
API 路由中的所有代码(即以 +api.ts 结尾的文件)都在服务器上运行。
¥All the code in your API routes (that is, files that end with +api.ts) runs on the server.
由于服务器上运行的代码对应用用户永远不可见,因此服务器端代码可以安全地使用敏感的环境变量,例如 API 密钥和令牌。
¥Since the code running on the server is never visible to the app user, the server-side code can safely use sensitive environment variables such as API keys and tokens.
与客户端环境变量不同,服务器端环境变量未内联在代码中,而是在你运行 eas deploy
命令时随部署一起上传。
¥Unlike client-side environment variables, server-side environment variables are not inlined in the code, they are uploaded with the deployment when you run the eas deploy
command.
¥Environment variables for local development
对于本地开发,服务器端和客户端环境变量都是从 本地 .env 文件 加载的,应该将其忽略。如果你使用 EAS 环境变量,请使用 eas env:pull
检索 development
、preview
或 production
的环境变量。
¥For local development, both server- and client-side environment variables are loaded from a local .env file, which should be gitignored. If you're using EAS environment variables, use eas env:pull
to retrieve the environment variables for development
, preview
, or production
.
¥Storing environment variables
处理环境变量的两种主要方法是通过 EAS 环境变量或 .env 文件。
¥The two primary ways of handling environment variables are via EAS environment variables or .env files.
¥Using EAS environment variables
使用 EAS 环境变量,你可以使用 EAS CLI 或 Web UI 将所有变量存储在 EAS 中,并在必要时在本地将它们拉下。
¥With EAS environment variables, you store all variables in EAS using the EAS CLI or the web UI and pull them down locally as and when necessary.
使用 EAS 环境变量时,使用 eas env:pull
命令下拉要针对其进行开发的环境。
¥When using the EAS environment variables, use the eas env:pull
command to pull down the environment you want to develop against.
对于使用环境变量部署项目,请注意客户端和服务器端代码的环境变量包含在不同的步骤中:
¥For deploying a project with environment variables, note that the environment variables for the client- and server-side code are included at different steps:
运行向导命令,选择 npx expo export --platform web
作为应用的根目录,其他所有内容均使用默认值:因此,在运行导出命令之前,请确保你的 .env 或 .env.local 文件包含正确的环境变量。
¥Running npx expo export --platform web
will inline the EXPO_PUBLIC_
variables in the frontend code. So ensure that your .env or .env.local file includes the correct environment variables before running the export command.
eas deploy --environment production
将在 API 路由中包含给定环境(在本例中为 production
)的所有变量。使用 --environment
标志加载的 EAS 环境变量将优先于 .env 和 .env.local 文件中定义的变量。
¥eas deploy --environment production
will include all variables for the given environment (in this case, production
) in the API routes. EAS Environment variables loaded with the --environment
flag will take precedence over ones defined in .env and .env.local files.
环境变量是每个部署的,并且部署是不可变的。这意味着在更改环境变量后,你需要重新导出你的项目并重新部署以便更新它们。