--- icon: ri:settings-3-line --- # Configuration > Customize and extend Nitro defaults. ::read-more{to="/config"} See [config reference](/config) for available options. :: ## Config file You can customize Nitro with a configuration file. Depending on your setup, define options in `nitro.config.ts`, or under the `nitro` key in `vite.config.ts` when using the Vite plugin. ::code-group ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ // Nitro options }) ``` ```ts [vite.config.ts] import { defineConfig } from 'vite' import { nitro } from 'nitro/vite' export default defineConfig({ plugins: [ nitro() ], nitro: { // Nitro options } }) ``` :: ### Supported configuration files Nitro loads its configuration using [c12](https://github.com/unjs/c12) from the project root directory (the directory passed to the `nitro` CLI, defaulting to the current working directory). The following locations are checked in order and the first matching file is used: - `nitro.config.{js,ts,mjs,cjs,mts,cts,json,jsonc,json5,yaml,yml,toml}` - `.config/nitro.{js,ts,mjs,cjs,mts,cts,json,jsonc,json5,yaml,yml,toml}` - `.config/nitro.config.{js,ts,mjs,cjs,mts,cts,json,jsonc,json5,yaml,yml,toml}` Using `nitro.config.ts` is the recommended convention. An extensionless `.nitrorc` file in the same directory is also loaded (using `key=value` syntax) and merged with lower priority than the main configuration file. When using the Vite plugin, options can also be passed inline with the `nitro` key in `vite.config.ts` (as shown above). ### Environment-specific config Using [c12](https://github.com/unjs/c12) conventions, you can provide environment-specific overrides using `$development` and `$production` keys: ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ logLevel: 3, $development: { // Options applied only in development mode debug: true, }, $production: { // Options applied only in production builds minify: true, }, }) ``` The environment name is `"development"` during `nitro dev` and `"production"` during `nitro build`. Use the `--envName` flag to set a custom name, and the `$env` key for its overrides: ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ $env: { staging: { // Options applied only with `--envName staging` runtimeConfig: { apiBase: "https://staging.example.com" }, }, }, }) ``` ```sh npx nitro build --envName staging ``` Routes prerendered during that build apply the same overrides. Tools that use the [`nitro/builder`](/docs/utils#nitrobuilder) API can pass the name with `createNitro(config, { c12: { envName: "staging" } })`. The resolved name is available as `nitro.options.envName`, and as `import.meta.envName` in server code (replaced statically at build time): ```ts [routes/env.ts] import { defineHandler } from "nitro"; export default defineHandler(() => ({ env: import.meta.envName })); ``` ### Extending configs You can extend from other configs or presets using the `extends` key: ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ extends: "./base.config", }) ``` Resolved layers are available to [modules](/docs/modules) as `nitro.options.layers`, ordered from highest to lowest priority. The first layer is always the project root. Each layer has a `cwd` (layer directory), an optional `configFile`, its own `config` (before merging), and an optional `meta`. Presets are not included. ```ts [modules/layers.ts] import type { NitroModule } from "nitro/types"; export default { setup(nitro) { for (const layer of nitro.options.layers) { nitro.logger.info(`Layer: ${layer.meta?.name || layer.cwd}`); } }, } satisfies NitroModule; ``` ## Directory options Nitro provides several options for controlling directory structure: | Option | Default | Description | | --- | --- | --- | | `rootDir` | `.` (current directory) | The root directory of the project. | | `serverDir` | `false` | Server source directory (set to `"server"` or `"./"` to enable). | | `buildDir` | `node_modules/.nitro` | Directory for build artifacts. | | `output.dir` | `.output` | Production output directory. | | `output.serverDir` | `.output/server` | Server output directory. | | `output.publicDir` | `.output/public` | Public assets output directory. | ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ serverDir: "server", buildDir: "node_modules/.nitro", output: { dir: ".output", }, }) ``` ::note The `srcDir` option is deprecated. Use `serverDir` instead. :: :read-more{to="/config/directories"} ## Environment variables Certain Nitro behaviors can be configured using environment variables. These variables configure Nitro itself at build and startup time. To expose your own values to the application, use [runtime configuration](#runtime-configuration) instead. | Variable | Description | | --- | --- | | `NITRO_PRESET` | Override the deployment preset. | | `NITRO_COMPATIBILITY_DATE` | Set the compatibility date. | | `NITRO_APP_BASE_URL` | Override the base URL (default: `/`). | | `NITRO_BUILDER` | Select the bundler to use for building (`rollup`, `rolldown`, or `vite`). | | `NITRO_DEV_RELOAD_TIMEOUT` | How long (in milliseconds) `vite dev` requests wait for the server entry to finish loading or reloading before they fall back to the previous entry, or return a 503 if none has loaded yet (default: `120000`). | | `NITRO_ENV_PREFIX` | Set a custom secondary prefix for runtime config environment overrides (default: `_`). | | `NITRO_ENV_EXPANSION` | Enable environment variable expansion in runtime config values. | ## Runtime configuration Runtime config lets you expose configuration values to your application and override them at runtime using environment variables. This is useful for values that differ between environments (development, staging, production), such as API endpoints or feature flags. First, define the runtime config in your configuration file. ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ runtimeConfig: { apiToken: "dev_token", // `dev_token` is the default value } }); ``` You can now access the runtime config using `useRuntimeConfig()`. ```ts [api/example.get.ts] import { defineHandler } from "nitro"; import { useRuntimeConfig } from "nitro/runtime-config"; export default defineHandler((event) => { return useRuntimeConfig().apiToken; // Returns `dev_token` }); ``` ### Nested objects Runtime config supports nested objects. Keys at any depth are mapped to environment variables using the `NITRO_` prefix and `UPPER_SNAKE_CASE` conversion: ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ runtimeConfig: { database: { host: "localhost", port: 5432, }, }, }); ``` ```bash [.env] NITRO_DATABASE_HOST="db.example.com" NITRO_DATABASE_PORT="5433" ``` ::note Only keys defined in `runtimeConfig` in your config file will be considered. You cannot introduce new keys using environment variables alone. :: ### Serialization Runtime config values must be serializable (strings, numbers, booleans, plain objects, and arrays). Non-serializable values (class instances, functions, etc.) will trigger a warning at build time. Values that are `undefined` or `null` in the config are replaced with empty strings (`""`) as a fallback. ### Local development You can update the runtime config using environment variables. You can use a `.env` or `.env.local` file locally and use platform variables in production (see below). Create an `.env` file in your project root: ```bash [.env] NITRO_API_TOKEN="123" ``` Restart the development server, fetch the `/api/example` endpoint, and you should see `123` as the response instead of `dev_token`. ::note The `.env` and `.env.local` files are loaded when Nitro resolves your config, in both `nitro dev` and `nitro build`, so `nitro.config.ts` can read them via `process.env`. Variables already present in the environment take precedence over `.env` values. They are **not** loaded by the built server at runtime: in production, use your platform's native environment variable mechanism. :: You can still access environment variables directly using `import.meta.env` or `process.env`, but avoid reading them in ambient global contexts (at the top level of modules) to prevent unexpected behavior. ### Production You can define variables in your production environment to update the runtime config. ::warning All variables must be prefixed with `NITRO_` to be applied to the runtime config. They will override the runtime config variables defined within your `nitro.config.ts` file. :: ```bash NITRO_API_TOKEN="123" ``` Keys are camelCase in runtime config and UPPER_SNAKE_CASE in environment variables. ```ts { helloWorld: "foo" } ``` ```bash NITRO_HELLO_WORLD="foo" ``` ### Custom env prefix You can configure a secondary environment variable prefix using the `nitro.envPrefix` runtime config key. This prefix is checked in addition to the default `NITRO_` prefix: ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ runtimeConfig: { nitro: { envPrefix: "APP_", }, apiToken: "", }, }); ``` With this configuration, both `NITRO_API_TOKEN` and `APP_API_TOKEN` will be checked as overrides. ::note When no custom prefix is configured, the default secondary prefix is `_`. For example, `_API_TOKEN` also overrides `apiToken`. :: ### Env expansion When enabled, environment variable references using `{{VAR_NAME}}` syntax in runtime config string values are expanded at runtime: ```ts [nitro.config.ts] import { defineConfig } from "nitro"; export default defineConfig({ experimental: { envExpansion: true, }, runtimeConfig: { url: "https://{{APP_DOMAIN}}/api", }, }); ``` ```bash APP_DOMAIN="example.com" ``` At runtime, `useRuntimeConfig().url` will resolve to `"https://example.com/api"`.