From afe6e30678ee47e31451ecc7da6b4ab6932ddbe2 Mon Sep 17 00:00:00 2001 From: Ennio Visconti Date: Fri, 15 Jul 2022 10:55:03 +0200 Subject: [PATCH 1/6] Clarified how environment variables are loaded This change tries to clarify some approximate usage of the word runtime in explaining how the runtimeConfig API works. --- docs/content/2.guide/2.features/10.runtime-config.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 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 1c47da428dc..48f603813fa 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 config within your application and server routes, using also environment variables, with the ability to update them at runtime. ## Exposing runtime config @@ -35,14 +35,16 @@ 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, allowing the adoption of `.env` files also outside of a node environment (e.g. in SSG). -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. -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. :: -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`. +Runtime config values are automatically replaced by matching environment variables. For this to work, you _must_ have a fallback value (which can just be an empty string) defined in your `nuxt.config`. **Example:** From dfa4e060eae0700c5b5ebeece5e5675a3e2eb55d Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Sun, 17 Jul 2022 17:31:22 +0200 Subject: [PATCH 2/6] Update docs/content/2.guide/2.features/10.runtime-config.md --- docs/content/2.guide/2.features/10.runtime-config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 48f603813fa..033b8eb67d3 100644 --- a/docs/content/2.guide/2.features/10.runtime-config.md +++ b/docs/content/2.guide/2.features/10.runtime-config.md @@ -35,7 +35,7 @@ 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, allowing the adoption of `.env` files also outside of a node environment (e.g. in SSG). +Nuxt CLI has built-in [dotenv](https://github.com/motdotla/dotenv) when building, generating, and also in development mode. 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. From d8896e99fc59a99d258d88c23951d154d5fa7bde Mon Sep 17 00:00:00 2001 From: Ennio Visconti Date: Sun, 17 Jul 2022 16:05:28 +0000 Subject: [PATCH 3/6] added warning in environment alert box --- docs/content/2.guide/2.features/10.runtime-config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 033b8eb67d3..a04b039b28b 100644 --- a/docs/content/2.guide/2.features/10.runtime-config.md +++ b/docs/content/2.guide/2.features/10.runtime-config.md @@ -41,7 +41,7 @@ In addition to any process environment variables, if you have a `.env` file in y 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. +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`. :: Runtime config values are automatically replaced by matching environment variables. For this to work, you _must_ have a fallback value (which can just be an empty string) defined in your `nuxt.config`. From b429705145defe42a0f126d7bfa50bec143b4a57 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Sun, 17 Jul 2022 18:09:05 +0200 Subject: [PATCH 4/6] Update docs/content/2.guide/2.features/10.runtime-config.md --- docs/content/2.guide/2.features/10.runtime-config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a04b039b28b..523ca9ea2da 100644 --- a/docs/content/2.guide/2.features/10.runtime-config.md +++ b/docs/content/2.guide/2.features/10.runtime-config.md @@ -44,7 +44,7 @@ Alternatively, you could pass the environment variables as arguments using the t 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`. :: -Runtime config values are automatically replaced by matching environment variables. For this to work, you _must_ have a fallback value (which can just be an empty string) defined in your `nuxt.config`. +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`. **Example:** From d2009680d3039c2eb125e31beda8f1484890dda5 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 19 Jul 2022 18:54:21 +0100 Subject: [PATCH 5/6] docs: update --- docs/content/2.guide/2.features/10.runtime-config.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 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 523ca9ea2da..164a728dd57 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, using also environment variables, with the ability to update them at runtime. +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,13 +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) when building, generating, and also in development mode. +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` **at build time**, and therefore 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/generate time**, and any environment variables set there will be accessible within your `nuxt.config` file and modules. -Alternatively, you could pass the environment variables as arguments using the terminal, e.g. `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, 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`. +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`. From fb281191f3a335479f5d8f4963ba8651683110e1 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 21 Jul 2022 11:07:24 +0200 Subject: [PATCH 6/6] Apply suggestions from code review --- docs/content/2.guide/2.features/10.runtime-config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 164a728dd57..f415061e9a6 100644 --- a/docs/content/2.guide/2.features/10.runtime-config.md +++ b/docs/content/2.guide/2.features/10.runtime-config.md @@ -37,7 +37,7 @@ The most common way to provide configuration is by using [Environment Variables] ::alert{type=info} 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 **at build/dev/generate time**, and any environment variables set there will be 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. When updating `.env` in development mode, the Nuxt instance is automatically restarted to apply new values to the `process.env`.