Skip to main content

Quick start

Get up and running with Env Core in just a few minutes. This guide will walk you through setting up environment variable validation in your Node.js project.
1

Install Env Core

Install the package using your preferred package manager:
npm install env-core
2

Create your environment schema

Define a schema that describes your environment variables. Create a file like envSchema.js or include it directly in your application:
// envSchema.js
export const envSchema = {
  PORT: Number,
  NODE_ENV: String,
  DEBUG: {
    type: Boolean,
    default: false
  },
  DATABASE_URL: String,
  API_KEY: {
    type: String,
    required: false
  }
};
  • Use simple types (Number, String, Boolean) for required variables
  • Use objects with type, default, and required for more control
3

Create a .env file

Create a .env file in your project root with your environment variables:
PORT=3000
NODE_ENV=development
DEBUG=true
DATABASE_URL=postgresql://localhost:5432/mydb
Never commit your .env file to version control. Add it to .gitignore.
4

Validate your environment

Import and use validateEnv in your application entry point:
// index.js
import { validateEnv } from 'env-core';
import { envSchema } from './envSchema.js';

// Validate environment variables
const env = validateEnv(envSchema);

// Now use your validated environment variables
console.log(`Server will run on port ${env.PORT}`);
console.log(`Environment: ${env.NODE_ENV}`);
console.log(`Debug mode: ${env.DEBUG}`);
TypeScript users get full type inference - env.PORT is typed as number, env.NODE_ENV as string, etc.
5

Run your application

Run your application. If any required environment variables are missing or have invalid types, Env Core will print detailed error messages and exit:
node index.js
Success output:
Server will run on port 3000
Environment: development
Debug mode: true
Error output (if PORT is missing):
Environment validation failed:
- Missing required field: PORT

What happens during validation?

When you call validateEnv(), Env Core:
  1. Loads environment variables from your .env file (or uses process.env)
  2. Checks that all required variables are present
  3. Validates that each variable matches its expected type
  4. Applies default values for missing optional variables
  5. Returns a fully typed object with your validated environment
If validation fails, your application exits immediately with clear error messages indicating what’s wrong.

Next steps

Schema definition

Learn about all the ways to define your environment schema

Framework guides

See how to integrate Env Core with Express.js, NestJS, and more

Type safety

Understand how TypeScript type inference works

API reference

Explore the complete API documentation

Build docs developers (and LLMs) love