.env

.env 文件指定你的构建/开发时环境变量。
应将此文件添加到你的 .gitignore 文件中,以避免将机密推送到你的代码库。

开发、构建和生成时间

¥Dev, Build and Generate Time

Nuxt CLI 在开发模式以及运行 nuxi buildnuxi generate 时内置了 dotenv 支持。

¥Nuxt CLI has built-in dotenv support in development mode and when running nuxi build and nuxi generate.

除了任何进程环境变量之外,如果你的项目根目录中包含 .env 文件,它将在开发、构建和生成时自动加载。任何在那里设置的环境变量都可以在你的 nuxt.config 文件和模块中访问。

¥In addition to any process environment variables, if you have a .env file in your project root directory, it will be automatically loaded at dev, build and generate time. Any environment variables set there will be accessible within your nuxt.config file and modules.

.env
MY_ENV_VARIABLE=hello
请注意,从 .env 中删除变量或完全删除 .env 文件不会取消已设置的值。

自定义文件

¥Custom File

如果你想使用其他文件 - 例如,使用 .env.local.env.production - 你可以在使用 nuxi 时传递 --dotenv 标志来实现此目的。

¥If you want to use a different file - for example, to use .env.local or .env.production - you can do so by passing the --dotenv flag when using nuxi.

Terminal
npx nuxi dev --dotenv .env.local

在开发模式下更新 .env 时,Nuxt 实例会自动重启,并将新值应用于 process.env

¥When updating .env in development mode, the Nuxt instance is automatically restarted to apply new values to the process.env.

在你的应用代码中,你应该使用 Runtime Config 而不是简单的环境变量。

生产环境

¥Production

服务器构建完成后,你需要在运行服务器时设置环境变量。

¥After your server is built, you are responsible for setting environment variables when you run the server.

此时不会读取你的 .env 文件。具体操作方法因环境而异。

¥Your .env files will not be read at this point. How you do this is different for every environment.

此设计决策是为了确保跨各种部署环境的兼容性,其中一些部署环境可能没有可用的传统文件系统,例如无服务器平台或像 Cloudflare Workers 这样的边缘网络。

¥This design decision was made to ensure compatibility across various deployment environments, some of which may not have a traditional file system available, such as serverless platforms or edge networks like Cloudflare Workers.

由于 .env 文件未在生产环境中使用,因此你必须使用托管环境提供的工具和方法显式设置环境变量。以下是一些常用方法:

¥Since .env files are not used in production, you must explicitly set environment variables using the tools and methods provided by your hosting environment. Here are some common approaches:

  • 你可以使用终端将环境变量作为参数传递:
    $ DATABASE_HOST=mydatabaseconnectionstring node .output/server/index.mjs
  • 你可以在 Shell 配置文件(例如 .bashrc.profile)中设置环境变量。
  • 许多云服务提供商(例如 Vercel、Netlify 和 AWS)都提供了通过其仪表板、CLI 工具或配置文件设置环境变量的接口。

生产预览版

¥Production Preview

为了在本地生产环境中预览,我们建议使用 nuxi preview,因为使用此命令后,.env 文件将被加载到 process.env 中,以方便使用。 === 如需更高级的配置,请使用 nuxi preview。请注意,此命令需要在包目录中安装依赖。

¥For local production preview purpose, we recommend using nuxi preview since using this command, the .env file will be loaded into process.env for convenience. Note that this command requires dependencies to be installed in the package directory.

或者,你可以使用终端将环境变量作为参数传递。例如,在 Linux 或 macOS 上:

¥Or you could pass the environment variables as arguments using the terminal. For example, on Linux or macOS:

Terminal
DATABASE_HOST=mydatabaseconnectionstring node .output/server/index.mjs

请注意,对于纯静态站点,在项目预渲染后无法设置运行时配置。

¥Note that for a purely static site, it is not possible to set runtime configuration config after your project is prerendered.

Read more in Docs > Guide > Going Further > Runtime Config.
如果你想使用在构建时设置的环境变量,但不关心后续更新这些变量(或者只需要在应用中被动更新它们),那么 appConfig 可能是更好的选择。你可以在 nuxt.config 中(使用环境变量)定义 appConfig,也可以在项目的 ~/app.config.ts 文件中定义 appConfig。!!!IG1!!!