AdonisJS 5 Env Module: No Automatic Reload During Development
0 reputation · 23 Apr 2022, 12:35 UTC
0 reputation · 23 Apr 2022, 12:35 UTC
Achieve a repeatable development workflow by ensuring that changes to the .env file are reflected immediately while the AdonisJS 5 server is running.
The framework’s Env module loads environment variables once during application boot and caches them. Subsequent edits to .env are ignored until the process is restarted.
Automatic reloading would require a file‑watcher or a hot‑reload mechanism that is not present in the default dev server. Adding such a feature could impact startup performance or introduce race conditions in multi‑process setups.
Should AdonisJS 5 provide an out‑of‑the‑box mechanism to auto‑reload .env changes during development, or should developers rely on manual restarts or external tooling?
.env variables without compromising application stability?Env config cache used in production builds?In AdonisJS 5, the Env module loads environment variables once during the application boot process via the @ioc:Adonis/Core/Env provider. These values are cached in memory for the duration of the process. Consequently, changes made to the .env file while the server is running are not reflected until the process is manually restarted.
To implement automatic reloading of environment variables without a full process restart, the following architectural changes would be required:
Env provider would need to expose a reloadable binding that re-reads the .env file and updates the IoC container.Env.get() dynamically during execution. Any service that assigns an environment variable to a local property during its boot or register phase will continue to use the stale value regardless of a reload.fs.watch or a library like chokidar) would need to monitor the .env file and trigger the reload hook upon a save event.This mechanism must remain strictly isolated to development. In production, the @adonisjs/assembler creates a configuration cache that inlines environment values into the build. An auto-reload feature would bypass this optimization and introduce unnecessary file system overhead, so it must be disabled when NODE_ENV is set to production.
If you require this behavior today, the most stable approach is to rely on the built-in development server's watch mode, which restarts the process on file changes. If you are building a custom watcher, follow these scoped steps:
.env file.Env provider to clear the internal cache and re-parse the file.Env.get('KEY') inside request handlers rather than caching the value in a singleton service.Verification: To test if a reload is working, create a temporary route that returns Env.get('SOME_VAR'). Change the value in .env and refresh the browser; if the value updates without a terminal restart, the reload was successful.
Are you utilizing a process manager like PM2 in cluster mode during development? This would change the recommendation, as each cluster worker would attempt to watch the same file, potentially leading to race conditions and redundant CPU spikes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.