Quickstart

Let's get you started with a few simple steps.

Install ArkEnv

npm install arkenv arktype
pnpm add arkenv arktype
yarn add arkenv arktype
bun add arkenv arktype

ArkEnv works best when paired with ArkType.

Configure your project

Enable strict mode for the best typesafety, and use a modern TypeScript module resolution:

tsconfig.json
{
  "compilerOptions": {
    "strict": true, 
    "moduleResolution": "bundler" // or "node16" / "nodenext"
  }
}

You may omit moduleResolution if module is "Preserve", "Node16", or "NodeNext". See Requirements for details.

ArkType users: follow the ArkType setup to complete your configuration.

Define the schema

Add a schema to make your environment variables validated and typesafe.

We're using ArkType for this example, but you can mix and match any Standard Schema validator.

config/env.ts
import , {  } from 'arkenv';

export const  = ({
  // Optional variables with defaults
  : "'debug' | 'info' | 'warn' | 'error' = 'info'",
  
  // Arrays (comma-separated by default)
  : ("string[]").(() => ["localhost"]),
  : ("string[]").(() => []),
  
  // Optional environment variable
  "API_KEY?": 'string'
});

Define environment variables

Define some environment variables in a .env file in your project root:

.env
LOG_LEVEL=info
API_KEY=secret
ALLOWED_ORIGINS=http://localhost:3000,https://example.com

We recommend not committing the .env file to version control.

Use in your code

Import and use your validated and typed environment variables!

database.ts
import {  } from './config/env';

// Hover to see your validated config
const  = {
  : .,
  : .,
};

Next steps