
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
env-type-generator
Advanced tools
Zero-config TypeScript type generator for .env files with Zod validation, watch mode, and IDE autocomplete support
Zero-config TypeScript type generator for .env files with Zod validation, watch mode, and IDE autocomplete
Stop writing types for process.env manually. Let env-type-generator do it for you!
process.envnpm install --save-dev env-type-generator
# or
yarn add -D env-type-generator
# or
pnpm add -D env-type-generator
# Database
DATABASE_URL=postgresql://localhost:5432/mydb
DATABASE_POOL_SIZE=10
# API Keys
STRIPE_SECRET_KEY=sk_test_xxx
STRIPE_PUBLISHABLE_KEY=pk_test_xxx
# Features
ENABLE_ANALYTICS=true
MAX_UPLOAD_SIZE_MB=50
npx env-type-gen
// TypeScript knows about your env vars!
const dbUrl: string = process.env.DATABASE_URL; // Autocomplete works!
const poolSize: number = env.DATABASE_POOL_SIZE; // Parsed as number
// ❌ TypeScript will catch typos
const invalid = process.env.DATABSE_URL; // Error: Property 'DATABSE_URL' does not exist
npx env-type-gen [options]
| Option | Description | Default |
|---|---|---|
-e, --env-files <files...> | Environment files to parse | .env |
-o, --output <path> | Output path for type definitions | ./src/types/env.d.ts |
-v, --validation-lib <library> | Validation library (zod|yup|joi|none) | none |
-s, --validation-output <path> | Output path for validation schema | ./src/config/env.validator.ts |
-r, --required <vars...> | Required environment variables | [] |
-p, --parse-types | Parse and infer types from values | false |
-t, --strict | Treat all variables as required | false |
-w, --watch | Watch mode - regenerate on file changes | false |
-c, --config <path> | Path to config file | - |
# Generate types from .env
npx env-type-gen
# Specify custom output path
npx env-type-gen --output ./types/env.d.ts
# Parse multiple env files
npx env-type-gen --env-files .env .env.local
# Enable type inference (string → number, boolean, etc.)
npx env-type-gen --parse-types
This will generate:
declare namespace NodeJS {
interface ProcessEnv {
DATABASE_URL?: string | undefined;
DATABASE_POOL_SIZE?: number | undefined; // ← Inferred as number
ENABLE_ANALYTICS?: boolean | undefined; // ← Inferred as boolean
}
}
# Generate Zod validation schema
npx env-type-gen --validation-lib zod --parse-types
This generates env.validator.ts:
import { z } from 'zod';
export const envSchema = z.object({
DATABASE_URL: z.string().optional(),
DATABASE_POOL_SIZE: z.string().transform((val) => Number(val)).optional(),
ENABLE_ANALYTICS: z.enum(["true", "false"]).transform((val) => val === "true").optional(),
});
export type Env = z.infer<typeof envSchema>;
export const env = envSchema.parse(process.env);
# Mark specific variables as required
npx env-type-gen --required DATABASE_URL STRIPE_SECRET_KEY
# Or mark ALL variables as required (strict mode)
npx env-type-gen --strict
# Auto-regenerate on file changes
npx env-type-gen --watch
Create env-type.config.js:
module.exports = {
envFiles: ['.env', '.env.local'],
outputPath: './src/types/env.d.ts',
validationLib: 'zod',
validationOutput: './src/config/env.validator.ts',
requiredVars: ['DATABASE_URL', 'API_KEY'],
parseTypes: true,
strict: false,
};
Then run:
npx env-type-gen --config env-type.config.js
import { GeneratorService } from 'env-type-generator';
const service = new GeneratorService();
await service.generate({
envFiles: ['.env'],
outputPath: './types/env.d.ts',
parseTypes: true,
validationLib: 'zod',
validationOutput: './config/env.validator.ts',
});
# Generate types
npx env-type-gen --env-files .env.local .env --parse-types
# Add to package.json
{
"scripts": {
"dev": "env-type-gen && next dev",
"build": "env-type-gen && next build"
}
}
# Generate types
npx env-type-gen --parse-types
# Add to package.json
{
"scripts": {
"dev": "env-type-gen && vite",
"build": "env-type-gen && vite build"
}
}
# Generate with Zod validation
npx env-type-gen --validation-lib zod --parse-types --strict
# Use in your app
import { env } from './config/env.validator';
const app = express();
app.listen(env.PORT); // Type-safe!
Input (.env):
DATABASE_URL=postgresql://localhost:5432/mydb
PORT=3000
Output (env.d.ts):
declare namespace NodeJS {
interface ProcessEnv {
DATABASE_URL?: string | undefined;
PORT?: string | undefined;
}
}
export declare const env: {
DATABASE_URL?: string;
PORT?: string;
};
Input (.env):
DATABASE_URL=postgresql://localhost:5432/mydb
PORT=3000
ENABLE_DEBUG=true
CONFIG={"key":"value"}
Output (env.d.ts):
declare namespace NodeJS {
interface ProcessEnv {
DATABASE_URL?: string | undefined;
PORT?: number | undefined;
ENABLE_DEBUG?: boolean | undefined;
CONFIG?: object | undefined;
}
}
export declare const env: {
DATABASE_URL?: string;
PORT?: number;
ENABLE_DEBUG?: boolean;
CONFIG?: object;
};
Input (.env):
# Database connection string
DATABASE_URL=postgresql://localhost:5432/mydb
# Maximum number of connections
DATABASE_POOL_SIZE=10
Output (env.d.ts):
declare namespace NodeJS {
interface ProcessEnv {
/** Database connection string */
DATABASE_URL?: string | undefined;
/** Maximum number of connections */
DATABASE_POOL_SIZE?: number | undefined;
}
}
Add the generated types to your tsconfig.json:
{
"compilerOptions": {
"types": ["node"],
"typeRoots": ["./node_modules/@types", "./src/types"]
},
"include": ["src/**/*", "src/types/env.d.ts"]
}
| Feature | env-type-generator | t3-env | envalid | ts-dotenv |
|---|---|---|---|---|
| Auto-generate from .env | ✅ | ❌ | ❌ | ❌ |
| Zero config | ✅ | ❌ | ❌ | ❌ |
| Type inference | ✅ | ⚠️ | ⚠️ | ✅ |
| Runtime validation | ✅ | ✅ | ✅ | ✅ |
| Framework-agnostic | ✅ | ❌ | ✅ | ✅ |
| Watch mode | ✅ | ❌ | ❌ | ❌ |
| Active maintenance | ✅ | ✅ | ✅ | ❌ |
| Weekly downloads | TBD | 501K | 200K | 3.6K |
Key Advantage: We're the only tool that auto-generates types from existing .env files without requiring manual schema definition.
Cmd/Ctrl + Shift + P → "TypeScript: Restart TS Server"env.d.ts is included in tsconfig.jsonnpx env-type-genAdd to git ignore: Add generated files to .gitignore
src/types/env.d.ts
src/config/env.validator.ts
Generate in pre-build: Add to your build pipeline
{
"scripts": {
"prebuild": "env-type-gen",
"build": "tsc"
}
}
Use strict mode in production: Catch missing vars early
env-type-gen --strict --required DATABASE_URL API_KEY
Combine with dotenv-expand: For variable interpolation
BASE_URL=https://api.example.com
API_ENDPOINT=${BASE_URL}/v1
Contributions are welcome! Please read our Contributing Guide for details.
MIT © ashishyd
Made with ❤️ for the TypeScript community
FAQs
Zero-config TypeScript type generator for .env files with Zod validation, watch mode, and IDE autocomplete support
We found that env-type-generator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.