live-env
v1.0.0
Published
Hot-reload .env files into process.env without restarting Node.js. Supports multiple env files.
Maintainers
Readme
live-env
Hot-reload .env files into process.env without restarting Node.js.
Supports multiple env files, override priority, and a central config file.
Perfect for:
- Trading bots
- Long-running servers
- Workers
- AI inference systems
- Feature flags
- Runtime config updates
🚀 Install
npm install live-env⚡ Quick Start
Note:
liveEnv()returns a singleton watcher. You should only call it once, preferably at the very entry point of your application (e.g.,index.tsorserver.js). Subsequent calls will return the same watcher instance without changing the configuration.
ESM / TypeScript:
import { liveEnv } from "live-env";
liveEnv(); // watches .env by default
console.log(process.env.MY_KEY);CommonJS:
const { liveEnv } = require("live-env");
liveEnv(); // watches .env by default
console.log(process.env.MY_KEY);Edit .env → values update live.
🛑 Stopping the watcher
If you need to gracefully terminate the file watcher (e.g., during tests or graceful shutdown):
const env = liveEnv();
// Later...
env.close();📚 Watching multiple env files
Later files override earlier ones:
liveEnv({
files: [".env", ".env.local", ".env.dev"]
});Reload happens if any file changes.
⚙️ Auto-config via live-env.config.json
Create a live-env.config.json in your project root:
{
"files": [".env", ".env.local"]
}live-env will use this automatically unless overridden in code.
📡 Listen for events
On reload:
liveEnv().on("reload", (diff) => {
console.log("Updated:", diff);
});Example diff:
{
"added": { "NEW_KEY": "hello" },
"changed": { "MODE": "production" },
"removed": ["OLD_KEY"]
}On error: You can catch file watching/parsing errors to prevent them from crashing your app silently:
liveEnv().on("error", (err) => {
console.error("Live Env Error:", err);
});📝 License
MIT
Made with ❤️ by Gbiang B. Mashingil.
