This site counts visits with analytics cookies only if you accept. Read the consent

Skip to content

Getting StartedHow Courier Reads Your Project

How Courier Reads Your Project

What runs when the CLI, Studio, a bundler plugin or a mock server reads courier.config, which variables it has, and how the modules a framework's server answers itself — server-only, $env, astro:env — are answered outside that framework's build.

8 min readUpdated

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 itWhenThe variables it has
courier-cli routesOnce per runThe process's, then .env.development.local, .env.local, .env.development and .env — those there are — or the files --dotenv names
StudioAs it starts, and on every saveThe same files, read again when one is saved
A bundler plugin — withCourier(), the Vite plugins, the othersOn every build and dev server start, and again when a file the config read changesUnder 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 workerAs it startsStudio's
courier-mcpAs it starts and on every save, or once for --listThe process's, then the files --dotenv names
courier-serverAs it startsThe process's
attachRuntime()As your app startsYour 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:

src/env.ts
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:

FrameworkImportWhat it is when Courier reads the project
Next.jsserver-onlyAn empty module, as on Next's server layers: your definitions run on the server
Next.jsclient-onlyRefused: 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/privateThe variables without the PUBLIC_ prefix
SvelteKit$env/static/public, $env/dynamic/publicThe variables with the PUBLIC_ prefix
SvelteKit$app/environmentbrowser: false and building: true, as SvelteKit has them while it analyses the server during a build; dev unless NODE_ENV is production; version: ""
Astroastro:env/serverThe variables, and getSecret()
Astroastro:env/clientThe variables
  • Each variable is read from process.env when 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_; one svelte.config.js sets is not read, as that would run another of your files.
  • Astro coerces each variable to the type its env.schema declares and fills in its default. The schema lives in astro.config, which Courier does not run, so here every variable is the string the environment holds, and one it does not hold is undefined.
  • An import your tsconfig paths name 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 installed client-only is 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.
CodeWhat happened
MODULE_NOT_FOUNDAn 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_REFUSEDAn import the framework refuses where definitions run: Next's client-only
TRANSFORM_FAILEDA module does not compile — its syntax, JSX, a framework's own file format
EVALUATION_FAILEDThe 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_OUTThe 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, as RUNNER_TEMP, APPDATA and NODE_PATH hold, 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 as redact() cuts one, the environment's or not: a bearer token, a password in an address, a token= pair. Each is found in the message as the code wrote it, so neither hides the other: a runner's PGUSER=postgres masked inside postgres://app:pw@db/shop leaves 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's transform threw, a source no OpenAPI document could be written for, an agents policy the MCP server would not start with — in courier-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-cli prints the stack at COURIER_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: its code, its hint, and context.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_PHASE is definitions, as above — the switch your env library has: skipValidation for @t3-oss/env, or SKIP_ENV_VALIDATION=1 where 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/private in 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.