Managing environment variables in Next.js often leads to mistakes due to the lack of autocomplete or static typing for process.env
. To solve this, I forked and customized the original TS Env Typings extension to better suit modern Next.js workflows and eliminate common dev pitfalls.
undefined
bugs in process.env
..env.local
(or any env file you register).publicEnv.ts
and serverEnv.ts
.NEXT_PUBLIC_
) and server-only variables.process.env
.env-typings.json
.env-typings.json
file:{
"path": "./.env.local"
}
publicEnv.ts
(for NEXT_PUBLIC_*
)serverEnv.ts
(for private variables)import { PublicEnv } from './publicEnv';
console.log(PublicEnv.NEXT_PUBLIC_META_DEFAULT_TITLE);
with autocompletion and type checking.
publicEnv.ts
export class PublicEnv {
static readonly NEXT_PUBLIC_SITE_NAME = "My Site";
static readonly NEXT_PUBLIC_API_BASE = "https://api.example.com";
}
serverEnv.ts
export class ServerEnv {
static readonly DB_PASSWORD = "supersecret";
}