DEV Community

Michel Jee
Michel Jee

Posted on

How do you handle environment-specific configuration in your Node.js projects?

I've been using dotenv for years, but recently started experimenting with a more structured approach using JSON config files with environment overrides. Here's what I came up with:

javascript
const config = {
development: {
apiUrl: 'http://localhost:3000',
debug: true
},
production: {
apiUrl: 'https://api.example.com',
debug: false
}
};

const env = process.env.NODE_ENV || 'development';
const envConfig = config[env];

// Allow environment variables to override
for (const key in envConfig) {
if (process.env[key.toUpperCase()]) {
envConfig[key] = process.env[key.toUpperCase()];
}
}

export default envConfig;

This gives me a clean default config with the flexibility to override via env vars. I've been using a small library called EnvManager that adds validation and schema support. What's your preferred config management strategy?

Top comments (0)