What runs
courier.config.ts is a module like any other. To read your sources, Courier runs it and every module it imports, their top-level code included — the API modules, and whatever those import: an env module that checks your variables, a database client, an auth library. Several tools read it, each in a process of its own:
| Who reads it | When | The variables it has |
|---|---|---|
courier-cli routes | Once per run | The process's, then .env.development.local, .env.local, .env.development and .env — those there are — or the files --dotenv names |
| Studio | As it starts, and on every save | The same files, read again when one is saved |
A bundler plugin — withCourier(), the Vite plugins, the others | On every build and dev server start, and again when a file the config read changes | Under Vite, the env files Vite reads for the mode; under Next.js, webpack and the rest, the variables the framework's own process has |
| A mock server's worker | As it starts | Studio's |
courier-mcp | As it starts and on every save, or once for --list | The process's, then the files --dotenv names |
courier-server | As it starts | The process's |
attachRuntime() | As your app starts | Your app's |
A variable the process was started with wins over every file, and the first file to set a variable wins over the later ones.
A config may export a promise of what it defines, as one that awaits its setup does: every reading waits for it as part of the load, and one that rejects fails the load as code that throws does. Every reading runs your modules as your server would, and outside it. Keep the modules courier.config.ts imports to what defines your APIs: a module that connects to a database or calls a service when it is imported does so on every reading.
The definitions phase
Wherever Courier reads your project — courier-cli routes, courier-mcp --list, Studio and its commands, the process a bundler plugin reads the routes in — COURIER_PHASE holds definitions, as NEXT_PHASE names a phase of Next.js. Where your application runs — courier-server, courier-mcp serving an agent, attachRuntime(), the app itself — it is not set. Code that checks the environment as it is imported can skip the check then, where your API's definitions need none of what it checks:
import { createEnv } from "@t3-oss/env-nextjs"
import { z } from "zod"
export const env = createEnv({
server: { DATABASE_URL: z.string().url(), API_URL: z.string().url() },
runtimeEnv: { DATABASE_URL: process.env.DATABASE_URL, API_URL: process.env.API_URL },
skipValidation: !!process.env.SKIP_ENV_VALIDATION || process.env.COURIER_PHASE === "definitions",
})Courier sets the variable and decides nothing else for your code: a check it skips, a connection it does not open, is your code's to choose.
Modules your framework answers itself
A framework resolves some imports where it builds the server, never through Node: Next.js makes server-only an empty module on its server layers, SvelteKit and Astro write their env modules from the environment. A plain Node loader finds none of them, so Courier answers them as that framework's server does. It tells the framework from the package.json nearest the config, by the package it lists — next, @sveltejs/kit, astro — and answers in every module of your project it runs:
| Framework | Import | What it is when Courier reads the project |
|---|---|---|
| Next.js | server-only | An empty module, as on Next's server layers: your definitions run on the server |
| Next.js | client-only | Refused: the config's modules reached code for the browser, which Next refuses on the server. The load fails, naming the import |
| SvelteKit | $env/static/private, $env/dynamic/private | The variables without the PUBLIC_ prefix |
| SvelteKit | $env/static/public, $env/dynamic/public | The variables with the PUBLIC_ prefix |
| SvelteKit | $app/environment | browser: false and building: true, as SvelteKit has them while it analyses the server during a build; dev unless NODE_ENV is production; version: "" |
| Astro | astro:env/server | The variables, and getSecret() |
| Astro | astro:env/client | The variables |
- Each variable is read from
process.envwhen your code reads it, after the variables above are in place. A bundler plugin's reading depends on the variables your code named, so a change to another does not read the config again. - SvelteKit's prefix is its default,
PUBLIC_; onesvelte.config.jssets is not read, as that would run another of your files. - Astro coerces each variable to the type its
env.schemadeclares and fills in its default. The schema lives inastro.config, which Courier does not run, so here every variable is the string the environment holds, and one it does not hold isundefined. - An import your tsconfig
pathsname is yours: Courier follows your alias, not the framework's rule. - An installed package Node loads itself resolves its imports as Node does while that works. Where it fails — a library that imports
server-only, which Node cannot load — Courier runs the package itself, and answers its imports as it answers your modules'. A package Node loads without a failure keeps Node's answers: one that imports an installedclient-onlyis not refused.
When the project does not load
Every reader says why in the same words — courier-cli, Studio, a bundler plugin and a mock server alike: what failed, in which module, and the chain of imports from the config to it; then what to do.
src/server/db.ts imports "client-only": it is the marker of code for the browser, which Next.js refuses on the server, where courier.config is read (courier.config.ts → src/api.ts → src/server/db.ts).
Import that code from a client component alone, out of the modules courier.config imports.| Code | What happened |
|---|---|
MODULE_NOT_FOUND | An import did not resolve. Where it is one of a framework's own and Courier reads the project as another — $env/static/private in a project whose package.json lists no @sveltejs/kit — the hint names that framework; where the project's framework explains it — $lib before svelte-kit sync, ~/ before nuxi prepare, $app/navigation outside SvelteKit's server — the hint says what to do |
MODULE_REFUSED | An import the framework refuses where definitions run: Next's client-only |
TRANSFORM_FAILED | A module does not compile — its syntax, JSX, a framework's own file format |
EVALUATION_FAILED | The project's code threw while it ran. In a Nuxt project, a name Nuxt imports for you, such as useRuntimeConfig, is not defined outside Nuxt's build: definitions must not depend on Nuxt's runtime |
LOAD_TIMED_OUT | The config was still loading after its time — 60 s for courier-cli routes and a bundler plugin, 20 s for Studio — as a module waits for something that does not come |
- The project's own message is quoted with the environment's values masked as
[redacted]: each variable's value as it is, escaped in JSON, encoded for an address, trimmed, and each line of a value of several, such as a key in PEM — every such form of 8 characters or more. Left alone are the variables the system sets for every process —PATH,HOME, the user, the locale, npm's settings — and those that name places: an absolute path, or a list of them, asRUNNER_TEMP,APPDATAandNODE_PATHhold, read as the platform reads a path, and on Windows a name in any case. A variable whose name says it holds a secret —DB_PASSWORD,npm_config__auth— is masked whatever it holds, and so is a path with a run of 16 letters and digits mixing both, as a key or a token in a path has. Every credential is cut asredact()cuts one, the environment's or not: a bearer token, a password in an address, atoken=pair. Each is found in the message as the code wrote it, so neither hides the other: a runner'sPGUSER=postgresmasked insidepostgres://app:pw@db/shopleaves the password cut too. A shorter value is not masked, nor is a value the code changed some other way — cut, or encoded otherwise. Courier writes no value itself, only names. What the code threw is cut the same way, its stack included, wherever it is logged or shown, after the load too: in Studio and its commands — what an import'stransformthrew, a source no OpenAPI document could be written for, anagentspolicy the MCP server would not start with — incourier-cli's output, and in a bundler's terminal and error overlay, what a getter of the config threw as its sources were read among it. courier-cliprints the stack atCOURIER_LOG_LEVEL=debug; Studio shows the message on its APIs page and in its status bar.- A program reads the same through
ConfigLoadError, from@noverax/courier-build/config: itscode, itshint, andcontext.importChain, each module by its path as the system names it — with\on Windows — and an installed package by its name.
In CI
courier-cli routes --check runs your config in CI, where your application's variables usually are not:
- Skip the checks your definitions do not need while
COURIER_PHASEisdefinitions, as above — the switch your env library has:skipValidationfor@t3-oss/env, orSKIP_ENV_VALIDATION=1where your project reads it already. - Give the variables the definitions read, such as an API's
baseURL, with an env file of their own:courier-cli routes --check --dotenv .env.ci. A placeholder serves where the value only names an address the check never calls. - Keep the definitions' modules plain. A module the config imports that opens a connection or calls a service as it loads does so on every reading; open it in the function that needs it.
- Mark server modules as your framework does.
import "server-only"in a Next.js project,$env/static/privatein a SvelteKit one, are answered as their servers answer them.
A load that fails names the module and quotes its message masked by that rule, so the job's log holds a variable's value in none of the forms it lists.