|
| 1 | +--- |
| 2 | +title: Flat layout |
| 3 | +icon: Zap |
| 4 | +description: Get the ultimate DX in Nuxt using a single, unified flat schema file. |
| 5 | +--- |
| 6 | + |
| 7 | +In this setup, you define all your environment variables in a single flat object. |
| 8 | + |
| 9 | +```files |
| 10 | +env.ts |
| 11 | +``` |
| 12 | + |
| 13 | +To protect server-side secrets from ever reaching the client bundle in the browser, ArkEnv uses a proxy that throws a runtime error if a server-side variable is accessed in browser code. Note that during Server-Side Rendering (SSR), client-shared code executes on the server first, where this runtime guard is not active. See the [Security model](/docs/nuxt/security) page for more details. |
| 14 | + |
| 15 | +:::warning[Server logic in client bundle] |
| 16 | +Defining your schemas together means the *server validation logic* is shipped in the client bundle. |
| 17 | + |
| 18 | +| ⚠️ Visible in JS bundle | 🔒 Secure on server | |
| 19 | +| :------------------------------------------------------------------------------ | :--------------------------------------------------- | |
| 20 | +| **Schema** (keys, types, and constraints)<br />e.g., `DATABASE_URL: string.url` | **Runtime values**<br />e.g., `"postgresql://db..."` | |
| 21 | + |
| 22 | +The values themselves are *not* shipped to the client! But if exposing your server variable *names*, *types*, or *constraints* is a security concern, use the [strict layout](/docs/nuxt/layouts/strict) instead. |
| 23 | +::: |
| 24 | + |
| 25 | +## Setup |
| 26 | + |
| 27 | +The easiest way to bootstrap the flat layout is with the [ArkEnv CLI](/docs/cli). It automatically configures `@arkenv/nuxt` for your existing Nuxt project and generates your `env.ts` file. |
| 28 | + |
| 29 | +```npm |
| 30 | +npx arkenv@latest init |
| 31 | +``` |
| 32 | + |
| 33 | +## Your schema |
| 34 | + |
| 35 | +When bootstrapping with the CLI, it generates a single `env.ts` file where you define your environment variables directly in a flat structure: |
| 36 | + |
| 37 | +```ts title="env.ts" twoslash |
| 38 | +import arkenv from '@arkenv/nuxt'; |
| 39 | +// ---cut--- |
| 40 | +export const env = arkenv({ |
| 41 | + DATABASE_URL: "string", |
| 42 | + NUXT_PUBLIC_API_URL: "string", |
| 43 | + NODE_ENV: "'development' | 'production' | 'test' = 'development'", |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +### Exposing client variables |
| 48 | + |
| 49 | +By default, ArkEnv automatically identifies and exposes variables to the client based on two criteria: |
| 50 | + |
| 51 | +1. Keys prefixed with `NUXT_PUBLIC_` (e.g. `NUXT_PUBLIC_API_URL`). |
| 52 | +2. The `NODE_ENV` variable (which is implicitly shared to align with Nuxt's runtime config). |
| 53 | + |
| 54 | +If you have custom variables that do not follow the prefix convention but must be exposed to the client, you can specify them using the `exposeToClient` option: |
| 55 | + |
| 56 | +```ts title="env.ts" twoslash |
| 57 | +import arkenv from '@arkenv/nuxt'; |
| 58 | +// ---cut--- |
| 59 | +export const env = arkenv({ |
| 60 | + DATABASE_URL: "string", |
| 61 | + NUXT_PUBLIC_API_URL: "string", |
| 62 | + CUSTOM_VAR: "string", |
| 63 | +}, { |
| 64 | + exposeToClient: ["CUSTOM_VAR"] |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +<Accordions> |
| 69 | + <Accordion title="Manual setup"> |
| 70 | + ### Configure environment [step] [!toc] |
| 71 | + |
| 72 | + Create an `env.ts` file at the root of your project to define your schema. Import `arkenv` from `@arkenv/nuxt`: |
| 73 | + |
| 74 | + ```ts twoslash title="env.ts" |
| 75 | + import arkenv from '@arkenv/nuxt'; |
| 76 | + // ---cut--- |
| 77 | + export const env = arkenv({ |
| 78 | + DATABASE_URL: "string", |
| 79 | + NUXT_PUBLIC_API_URL: "string", |
| 80 | + NODE_ENV: "'development' | 'production' | 'test' = 'development'", |
| 81 | + }); |
| 82 | + ``` |
| 83 | + |
| 84 | + ### Register module in Nuxt config [step] [!toc] |
| 85 | + |
| 86 | + Add `@arkenv/nuxt/module` to your modules array in `nuxt.config.ts`. The module scans your schema at build time for validation and registers the public keys to Nuxt's `runtimeConfig`. At runtime, values are resolved dynamically from `useRuntimeConfig()` — no generated files are created or needed: |
| 87 | + |
| 88 | + ```ts title="nuxt.config.ts" |
| 89 | + export default defineNuxtConfig({ |
| 90 | + modules: ["@arkenv/nuxt/module"] |
| 91 | + }); |
| 92 | + ``` |
| 93 | + |
| 94 | + You can customize the schema path and validation behavior using optional parameters: |
| 95 | + |
| 96 | + ```ts title="nuxt.config.ts" |
| 97 | + export default defineNuxtConfig({ |
| 98 | + modules: ["@arkenv/nuxt/module"], |
| 99 | + arkenv: { |
| 100 | + schemaPath: "env.ts", // Path to your schema (default: env.ts or src/env.ts) |
| 101 | + layout: "flat", // Explicitly specify layout |
| 102 | + validate: true, // Validate environment variables at build-time (default: true) |
| 103 | + } |
| 104 | + }); |
| 105 | + ``` |
| 106 | + |
| 107 | + ### Use in your code [step] [!toc] |
| 108 | + |
| 109 | + Import `env` throughout your Nuxt application: |
| 110 | + |
| 111 | + ```ts title="components/MyComponent.vue" |
| 112 | + import { env } from "~~/env"; |
| 113 | + |
| 114 | + // Access is fully typesafe and autocompleted |
| 115 | + const apiUrl = env.NUXT_PUBLIC_API_URL; |
| 116 | + ``` |
| 117 | + |
| 118 | + If you accidentally attempt to access a server-side secret in client-side code, it will throw a descriptive runtime exception during browser execution (note that it will not throw during Server-Side Rendering, only once hydration completes in the browser): |
| 119 | + |
| 120 | + ```ts title="components/ClientOnlyComponent.vue" |
| 121 | + import { env } from "~~/env"; |
| 122 | + |
| 123 | + // Throws a runtime error if evaluated in client-side code |
| 124 | + const dbUrl = env.DATABASE_URL; |
| 125 | + ``` |
| 126 | + </Accordion> |
| 127 | +</Accordions> |
0 commit comments