Route rules don't accept .env variables
Environment
[email protected] [email protected]
Backend with Nitro CLI setting up routeRules cors
// nitro.config.ts
import { defineConfig } from 'nitro';
export default defineConfig({
serverDir: './server',
routes: {
'/api/auth/**': './server/utils/auth.ts'
},
routeRules: {
'/api/auth/**': {
cors: {
origin: [process.env['APP_URL']!],
// origin: (origin) => origin === process.env['APP_URL'],
credentials: true,
},
},
},
});
Reproduction
https://stackblitz.com/edit/github-6ej7nrwe?file=vite.config.ts
Following the better-auth nitro integration and the section CORS by replacing the hard coded origin URL with an env variable.
I am trying it out with better-auth and nitro in this repo: https://github.com/marcjulian/aba/commit/645f78d4b6dacd38728aafa3f444710e8019ce0d
Describe the bug
nitro build fails during CI when the h3 routeRules uses process.env and .env is not provided in CI.
nitro build also fails when using origin: (origin) => boolean function.
This happens because the rules are compiled and must survive a JSON round-trip.
Hard coding the origin URL doesn't allow to host the nitro API in different environments (staging, production) where different URLs are being used.
Using a middleware for applying CORS is the current solution to use .env variables during runtime.
// server/middleware/cors.ts
import env from '#server/utils/env.ts';
import { defineMiddleware } from 'nitro';
import { handleCors } from 'nitro/h3';
export default defineMiddleware((event) => {
// Only apply CORS to the API routes.
if (!event.url.pathname.startsWith('/api/')) {
return undefined;
}
const corsRes = handleCors(event, {
origin: (origin) => origin === process.env['APP_URL'],
credentials: true,
});
// If this was a preflight request, handleCors has sent the 204 response.
// Return it to short-circuit further handling.
if (corsRes !== false) {
return corsRes;
}
// Otherwise, continue to the next middleware / route handler.
return undefined;
});
Additional context
No response
Logs
origin: [process.env['APP_URL']!]
nitro build
✔ Generated public .output/public nitro 12:42:04 PM [nitro 12:42:04 PM] ℹ Building server (builder: rolldown, preset: node-server, compatibility date: 2026-09-04) Error: Build failed with 1 error:
[plugin nitro:virtual]
Error: [h3] rules: compiler: cors rule for /api/** has a non-JSON-serializable value at options.origin[0] (undefined) — compiled rule options must survive a JSON round-trip
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:100:8)
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:88:44)
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:95:4)
at file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:38:3
at Array.map (
origin: (origin) => origin === process.env['APP_URL']
nitro build
Runtime config option nitro.routeRules./api/**.cors.origin may not be able to be serialized.
✔ Generated public .output/public nitro 12:41:25 PM
ℹ Building server (builder: rolldown, preset: node-server, compatibility date: 2026-09-04) nitro 12:41:25 PM
Error: Build failed with 1 error:
[plugin nitro:virtual]
Error: [h3] rules: compiler: cors rule for /api/** has a non-JSON-serializable value at options.origin (function) — compiled rule options must survive a JSON round-trip
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:100:8)
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:95:4)
at file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:38:3
at Array.map (
Looking into this — will follow up with a fix + failing test.