Environment Variables

srvkit supports deferred environment variables — values that are resolved at runtime instead of being hardcoded at build time. This lets you configure different options from environment variables without embedding them in the output bundle.

How It Works

srvkit uses Envkist under the hood. In development, env references resolve immediately by reading process.env etc. In production, they are injected as runtime-appropriate code that reads the environment variable when the server starts.

Usage

Import env from the plugin entry:

Vite
Rsbuild
vite.config.ts
import { defineConfig } from "vite";
import { srvkit, env } from "@srvkit/vite/plugin";

export default defineConfig({
    plugins: [
        srvkit({
            build: {
                host: env.string("HOST"),
                port: env.number("PORT"),
            },
        }),
    ],
});

env.string

Creates a resolvable string value that reads from an environment variable:

env.string("HOST");

env.number

Creates a resolvable number value that reads from an environment variable:

env.number("PORT");

With Fallback Values

Pass a second argument to set a default when the environment variable is not set:

env.string("HOST", "0.0.0.0");
env.number("PORT", 8080);

Static Values

You can still use plain strings and numbers — they are fully supported alongside env references:

srvkit({
    build: {
        host: env.string("HOST", "0.0.0.0"),
        port: 4000,
    },
});

Supported Options

The following options accept either a static value or an env reference:

OptionTypeEnv Function
dev.hoststringenv.string()
dev.portnumberenv.number()
dev.https.certstringenv.string()
dev.https.keystringenv.string()
dev.https.passphrasestringenv.string()
build.hoststringenv.string()
build.portnumberenv.number()
build.https.certstringenv.string()
build.https.keystringenv.string()
build.https.passphrasestringenv.string()

Runtime Behavior

In production builds, srvkit injects runtime-specific code for reading environment variables based on the runtime option:

RuntimeGenerated Code
nodeprocess.env.HOST
denoDeno.env.get("HOST")
bunBun.env.HOST
workerdenv.HOST (imported from cloudflare:workers)

When targeting workerd (Cloudflare Workers), srvkit automatically injects import { env } from "cloudflare:workers" into the output entry.