General Node.js usage
Env Core works with any Node.js application, regardless of the framework you’re using. This guide covers general usage patterns that apply to all Node.js projects.Basic usage
The simplest way to use Env Core is to callvalidateEnv at the start of your application:
Organizing your configuration
For larger projects, organize your environment configuration in a dedicated module:config/env.js
index.js
Using with different module systems
Env Core supports both ESM and CommonJS:- ESM (import)
- CommonJS (require)
TypeScript support
For TypeScript projects, Env Core provides full type inference:config/env.ts
index.ts
Environment-specific configuration
You can use different .env files for different environments:config/env.js
Using with worker threads
When using worker threads, validate the environment in the main thread and pass the validated config to workers:main.js
worker.js
Using with child processes
Child processes inherit environment variables from the parent:Testing patterns
For testing, use a separate test environment file:test/setup.js
test.env
test/example.test.js
CLI applications
For CLI applications, validate environment before running commands:bin/cli.js
Serverless functions
In serverless environments, validate once at cold start:functions/api.js
Error handling patterns
Env Core validates at startup, so no error handling is needed in your business logic:Best practices
Validate once at startup
Validate once at startup
Call
validateEnv once at application startup, not in every module:Export the validated environment
Export the validated environment
Export the validated
env object from a central module:Keep .env files out of version control
Keep .env files out of version control
Add
.env to your .gitignore but maintain a .env.example file:.gitignore
.env.example
Document your environment variables
Document your environment variables
Add comments to your schema explaining each variable:
Next steps
Express.js integration
See framework-specific integration with Express.js
Examples
Explore more usage examples