Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(config): add runtime config and environment variables section #1550

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Changes from all 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
81 changes: 81 additions & 0 deletions docs/content/1.guide/1.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,84 @@ timing=true
nitro.timing=true
```
::

## Runtime Configuration

Nitro provides a runtime config API to expose configuration within your application, with the ability to update it at runtime by setting environment variables.

To expose config and environment variables to the rest of your app, you will need to define runtime configuration in your configuration file.

**Example:**

::code-group
```ts [nitro.config.ts]
export default defineNitroConfig({
runtimeConfig: {
helloThere: "foobar",
}
})
```

```ts [nuxt.config.ts]
export default defineNuxtConfig({
runtimeConfig: {
helloThere: "foobar",
}
})
```
::

You can now access the runtime config using `useRuntimeConfig(event)`.

**Example:**

::code-group
```ts [api/example.get.ts (nitro)]
export default defineEventHandler((event) => {
return useRuntimeConfig(event).helloThere // foobar
});
```

```ts [server/api/example.get.ts (nuxt)]
export default defineEventHandler((event) => {
return useRuntimeConfig(event).helloThere // foobar
});
```
::

::alert{type="info"}
**Note:** Consider using `useRuntimeConfig(event)` within event handlers and utilities and avoid calling it in ambient global contexts.
::


### Environment Variables

Nitro supports defining environment variables using `.env` file in development (use platform variables for production).

Create an `.env` file in your project root:

```bash [.env]
TEST="123"
```

You can universally access environment variables using `import.meta.env.TEST` or `process.env.TEST`.

::alert{type="info"}
**Note:** Consider writing any logic that depends on environment variables within event handlers and utilities and avoid accessing and caching them in ambient global contexts.
::

### Update runtime config using environment variables

The variables prefixed with `NITRO_` will be applied to runtime config, and they will override the variables defined within your `nitro.config.ts` file. (matching "camelCase" version).

**Example:**

::code-group
```bash [.env (nitro)]
NITRO_HELLO_THERE="123"
```

```bash [.env (nuxt)]
NUXT_HELLO_THERE="123"
```
::