@arundo/typed-env
Typed environment variables using Zod.
Installation
npm install @arundo/typed-env
yarn add @arundo/typed-env
pnpm add @arundo/typed-env
Usage
Create a environment file with a schema and use typeEnvironment
to create a typed environment object:
import { z } from 'zod';
import { typeEnvironment } from '@arundo/typed-env';
export const envSchema = z.object({
NODE_ENV: z.enum(['test', 'development', 'production']),
PORT: z.coerse.number().int().default(3000),
});
export const environment = typeEnvironment(envSchema);
Import the environment object and use it:
import { environment } from './environment';
console.log(environment.NODE_ENV);
console.log(environment.PORT);
Set naming convention of environment variables:
export const environment = typeEnvironment(envSchema, { transform: 'camelcase' });
import { environment } from './environment';
console.log(environment.nodeEnv);
console.log(environment.port);
If you for some reason need to access the raw environment object, you can add types like this:
import { z } from 'zod';
import { typeEnvironment } from '@arundo/typed-env';
export const envSchema = z.object({
PORT: z.coerse.number().int().default(3000),
});
export const environment = typeEnvironment(envSchema, { transform: 'camelcase' });
declare global {
namespace NodeJS {
interface ProcessEnv extends Record<keyof z.infer<typeof envSchema>, string | undefined> {}
}
}
console.log(process.env.PORT);