Configuration
You can customize your Nitro builder with a configuration file.
export default defineNitroConfig({
// Nitro options
})
nitro
option in your Nuxt config instead..nitrorc
file in current working directory or in the user's home directory.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. This is useful when you want to expose different configuration values for different environments (e.g. development, staging, production). For example, you can use this to expose different API endpoints for different environments or to expose different feature flags.
First, you need to define the runtime config in your configuration file.
export default defineNitroConfig({
runtimeConfig: {
apiToken: "dev_token", // `dev_token` is the default value
}
})
You can now access the runtime config using useRuntimeConfig(event)
. Use useRuntimeConfig(event)
within event handlers and utilities and avoid calling it in ambient global contexts. This could lead to unexpected behavior such sharing the same runtime config across different requests.
export default defineEventHandler((event) => {
return useRuntimeConfig(event).apiToken // Returns `dev_token`
});
Local development
Finally, you can update the runtime config using environment variables. You can use a .env
file in development and use platform variables in production (see below).
Create an .env
file in your project root:
NITRO_API_TOKEN="123"
Re-start the development server, fetch the /api/example
endpoint and you should see 123
as the response instead of dev_token
.
Do not forget that you can still universally access environment variables using import.meta.env
or process.env
but avoid using them in ambiant global contexts 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"
In runtime config, define key using camelCase. In environment variables, define key using snake_case and uppercase.
{
helloWorld: "foo"
}
NITRO_HELLO_WORLD="foo"