Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

docs(runtime-config): clarify how environment variables are loaded #5916

Merged
merged 7 commits into from
Jul 21, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/content/2.guide/2.features/10.runtime-config.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Runtime Config

Nuxt provides a runtime config API to expose config within your application and server routes with the ability to update them at runtime using environment variables.
Nuxt provides a runtime config API to expose config within your application and server routes, using also environment variables, with the ability to update them at runtime.
danielroe marked this conversation as resolved.
Show resolved Hide resolved

## Exposing runtime config

Expand Down Expand Up @@ -35,11 +35,13 @@ console.log(runtimeConfig.public.apiBase)
The most common way to provide configuration is by using [Environment Variables](https://medium.com/chingu/an-introduction-to-environment-variables-and-how-to-use-them-f602f66d15fa).

::alert{type=info}
Nuxt CLI has built-in [dotenv](https://github.com/motdotla/dotenv) support.
Nuxt CLI has built-in [dotenv](https://github.com/motdotla/dotenv) when building, generating, and also in development mode.
danielroe marked this conversation as resolved.
Show resolved Hide resolved

In addition to any process environment variables, if you have a `.env` file in your project root directory, it will be automatically loaded into `process.env` and accessible within your `nuxt.config` file and modules.
In addition to any process environment variables, if you have a `.env` file in your project root directory, it will be automatically loaded into `process.env` **at build time**, and therefore accessible within your `nuxt.config` file and modules.
danielroe marked this conversation as resolved.
Show resolved Hide resolved

However, **after your project is built**, you are responsible for setting environment variables when you run the server - your `.env` file will not be read at this point. For example, you could pass the environment variables as arguments using the terminal `DATABASE_HOST=mydatabaseconnectionstring node .output/server/index.mjs`.
Alternatively, you could pass the environment variables as arguments using the terminal, e.g. `DATABASE_HOST=mydatabaseconnectionstring node .output/server/index.mjs`.

However, at **runtime** (i.e. while the application is running), your initial configuration files will not be read anymore, and only the current value accessible via the `runtimeConfig` API will be accessible. When updating `.env` in development mode, the Nuxt instance is automatically restarted to apply new values to the `process.env`.
danielroe marked this conversation as resolved.
Show resolved Hide resolved
::

Runtime config values are automatically replaced by matching environment variables at runtime. For this to work, you _must_ have a fallback value (which can just be an empty string) defined in your `nuxt.config`.
Expand Down