Skip to content

Getting Started

  1. Install the dependencies

    pnpm add @layerfig/config
  2. Define your files

    • Directoryapp
      • Directoryconfig
        • base.json
      • Directorysrc
        • Directoryconfig
          • index.ts
    import { ConfigBuilder, FileSource } from "@layerfig/config";
    
    export const config = new ConfigBuilder({
      validate: (finalConfig, z) =>
        z.object({
            appURL: z.url(),
            port: z.coerce.number().check(z.int(), z.positive()),
          })
          .parse(finalConfig),
    })
      .addSource(new FileSource("base.json"))
      .build();
    
  3. Use your type-safe config

    Now, you can access the configuration anywhere in your server-side code:

    import { config } from "./config";
    
    config.appURL; // string => "http://localhost:3000"
    config.port; // number => 3000

That is a single layer. The point of Layerfig is stacking several:

Requirement Detail
Zod v4 — bundled, and re-exported as z
Parser packages @layerfig/parser-* v6 pairs with @layerfig/config v3
Module formats ESM and CommonJS on the server; ESM only for /client
Runtime Any runtime with node:fs and node:path — see Deno