Skip to content

Conversation

@shivarm
Copy link
Contributor

@shivarm shivarm commented Dec 16, 2025

Giving a try

This example demonstrates how to use Express's built-in environment variable loading from .env files.

Automatically loads .env, .env.[NODE_ENV], and .env.local

import express from "express";

express.loadEnv();

Enable watch mode

import express from 'express';

 
const unwatch = express.loadEnv({
  watch: true,
  onChange: (changed, loaded) => {
    console.log('Environment variables changed:', changed);
  },
  onError: (err) => {
    console.error('Error reloading .env:', err.message);
  }
});

// Cleanup on shutdown
process.on('SIGINT', () => {
  unwatch();
  process.exit();
});

@krzysdz
Copy link
Contributor

krzysdz commented Dec 16, 2025

Modern Node.js versions support loading .env files:

I'm not sure Express should do something that can be easily done using standard Node.js functionality and that isn't really connected to serving things over HTTP.

@shivarm shivarm marked this pull request as draft December 17, 2025 04:25
@shivarm shivarm force-pushed the feature/native-env-file-support branch 2 times, most recently from 0e1d3f4 to 9957795 Compare December 17, 2025 04:54
@shivarm shivarm marked this pull request as ready for review December 17, 2025 05:01
@shivarm
Copy link
Contributor Author

shivarm commented Dec 17, 2025

I understand the concern about scope, and I agree Express should stay focused on HTTP. However, I believe this feature complements Express's developer experience. it can watch the realtime changes in .env files, for more info you can run the example project locally and by default watch mode code is commented in example but you can read about it.

@shivarm shivarm force-pushed the feature/native-env-file-support branch from 9957795 to a93be83 Compare December 17, 2025 15:11
@shivarm shivarm force-pushed the feature/native-env-file-support branch from a93be83 to c0a11a8 Compare December 17, 2025 15:13
Copy link
Member

@bjohansebas bjohansebas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn’t be handled by Express, as already mentioned, Node.js already does this, and since Express is minimalistic, I don’t think things like this should be added.

@shivarm shivarm closed this Dec 18, 2025
@shivarm shivarm deleted the feature/native-env-file-support branch December 18, 2025 04:14
@shivarm shivarm restored the feature/native-env-file-support branch December 18, 2025 04:14
@shivarm shivarm deleted the feature/native-env-file-support branch December 18, 2025 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants