From 2e794e7a19e1fa92ad9cde0c6c052b77df308bc1 Mon Sep 17 00:00:00 2001 From: Ennio Visconti Date: Thu, 21 Jul 2022 11:08:37 +0200 Subject: [PATCH] docs(runtime-config): clarify how environment variables are loaded (#5916) Co-authored-by: pooya parsa Co-authored-by: Daniel Roe --- docs/content/2.guide/2.features/10.runtime-config.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/content/2.guide/2.features/10.runtime-config.md b/docs/content/2.guide/2.features/10.runtime-config.md index 1c47da428d..f415061e9a 100644 --- a/docs/content/2.guide/2.features/10.runtime-config.md +++ b/docs/content/2.guide/2.features/10.runtime-config.md @@ -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 configuration within your application and server routes, with the ability to update it at runtime by setting environment variables. ## Exposing runtime config @@ -35,11 +35,15 @@ 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) support in development mode and when running `nuxi build` and `nuxi generate`. -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 **at build, dev, and generate time**, and any environment variables set there will be accessible within your `nuxt.config` file and modules. -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`. +When updating `.env` in development mode, the Nuxt instance is automatically restarted to apply new values to the `process.env`. + +However, **after your server is built**, you are responsible for setting environment variables when you run the server. Your `.env` file will not be read at this point. How you do this is different for every environment. On a Linux server, you could pass the environment variables as arguments using the terminal `DATABASE_HOST=mydatabaseconnectionstring node .output/server/index.mjs`. Or you could source your env file using `source .env && 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. :: 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`.