Configuration

Customize and extend Nitro defaults.
See config reference 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.

import { defineConfig } from "nitro";

export default defineConfig({
  // Nitro options
})
Nitro loads the configuration using c12, giving more possibilities such as using a .nitrorc file in the current working directory or in your home directory.

Environment-specific config

Using c12 conventions, you can provide environment-specific overrides using $development and $production keys:

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.

Extending configs

You can extend from other configs or presets using the extends key:

nitro.config.ts
import { defineConfig } from "nitro";

export default defineConfig({
  extends: "./base.config",
})

Config from package.json

You can also provide Nitro configuration under the nitro key in your package.json file.

Directory options

Nitro provides several options for controlling directory structure:

OptionDefaultDescription
rootDir. (current directory)The root directory of the project.
serverDirfalseServer source directory (set to "server" or "./" to enable).
buildDirnode_modules/.nitroDirectory for build artifacts.
output.dir.outputProduction output directory.
output.serverDir.output/serverServer output directory.
output.publicDir.output/publicPublic assets output directory.
nitro.config.ts
import { defineConfig } from "nitro";

export default defineConfig({
  serverDir: "server",
  buildDir: "node_modules/.nitro",
  output: {
    dir: ".output",
  },
})
The srcDir option is deprecated. Use serverDir instead.
Read more in 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 instead.

VariableDescription
NITRO_PRESETOverride the deployment preset.
NITRO_COMPATIBILITY_DATESet the compatibility date.
NITRO_APP_BASE_URLOverride the base URL (default: /).
NITRO_BUILDERSelect the bundler to use for building (rollup, rolldown, or vite).
NITRO_ENV_PREFIXSet a custom secondary prefix for runtime config environment overrides (default: _).
NITRO_ENV_EXPANSIONEnable 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.

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().

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:

import { defineConfig } from "nitro";

export default defineConfig({
  runtimeConfig: {
    database: {
      host: "localhost",
      port: 5432,
    },
  },
});
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 in development and use platform variables in production (see below).

Create an .env file in your project root:

.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.

The .env and .env.local files are only loaded during development (nitro dev). 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.

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.
NITRO_API_TOKEN="123"

Keys are camelCase in runtime config and UPPER_SNAKE_CASE in environment variables.

{
  helloWorld: "foo"
}
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:

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.

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:

nitro.config.ts
import { defineConfig } from "nitro";

export default defineConfig({
  experimental: {
    envExpansion: true,
  },
  runtimeConfig: {
    url: "https://{{APP_DOMAIN}}/api",
  },
});
APP_DOMAIN="example.com"

At runtime, useRuntimeConfig().url will resolve to "https://example.com/api".