AdonisJS 6 @adonisjs/env precedence when shell variables already exist
0 reputation · 23 May 2021, 12:55 UTC
0 reputation · 23 May 2021, 12:55 UTC
Goal: keep a repeatable AdonisJS 6 development environment where every developer starts with the same validated environment values without committing .env.
@adonisjs/env validates variables at boot from a project schema, and projects typically commit only .env.example. The dotenv loader generally leaves existing process variables intact, so an exported shell variable with the same name can shadow the .env value before validation runs. AdonisJS 6 does not appear to provide an Ace command to lock or export a validated environment snapshot from the schema.
This is specific to AdonisJS 6; AdonisJS 5 may not include @adonisjs/env, and the loader precedence needs confirmation for a given project.
Does shell export take precedence over .env during AdonisJS 6 boot? Is there a supported way to fail or warn when a process variable shadows a schema-defined value? Can a validated environment snapshot be generated from the schema for CI and local setup?
35775 reputation · 23 May 2021, 13:11 UTC
When a shell variable shares a name with a key defined in an @adonisjs/env schema, the Node.js dotenv loader that AdonisJS 6 relies on (by default) leaves the existing process.env entry intact. This means an exported shell variable with a matching name shadows the .env value before Env.fromSchema(schema) runs validation, so the schema validates against the shell value rather than the committed .env entry.
Confirmed behavior:
process.env; an exported shell variable with a matching name takes precedence.@adonisjs/env validates the current process.env state after loading; it does not compare loaded values against the .env file, so shadowing goes undetected by validation.Practical steps for this case:
export APP_PORT=9999) and start the AdonisJS server; check the logged port to confirm it uses the exported value.Env.fromSchema(), and logs the validated keys. Execute with and without the shell export to observe the difference.env.snapshot.json) for CI reproducibility.To enforce a repeatable environment, avoid exporting variables that appear in the schema, or use a bootstrap script that unsets relevant process.env keys before loading .env. If your project commonly exports shell variables that match schema keys, let me know; I can suggest a guard script tailored to your setup.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.