Skip to content

--env-file does not load .env from parent directories as documented #32683

@quentinadam

Description

@quentinadam

Version: Deno 2.7.5

Summary

According to the documentation, the --env-file flag should automatically load the first .env file found in the current directory or recursively in parent directories. However, in practice it appears to only check the current working directory.

Documentation reference

https://docs.deno.com/runtime/reference/env_variables/

"Deno will look for a .env file in the current working directory or recursively in parent directories."

Reproduction

Directory structure:

parent-folder
  .env
  child-folder
    main.ts

Example .env:

FOO=bar

Example main.ts:

console.log(Deno.env.get("FOO"));

Run from child-folder:

deno run -A --env-file main.ts

Expected behavior

Deno should discover and load the .env file located in the parent directory (../.env), resulting in:

bar

Actual behavior

The environment variable is not loaded:

undefined

The .env file is only loaded when explicitly specifying the path:

deno run -A --env-file=../.env main.ts

Environment

deno --version

2.7.5

Additional context

Based on the documentation wording, the --env-file flag appears intended to behave similarly to other tools that search for .env files up the directory tree (e.g., many dotenv implementations). If the current behavior is intentional, the documentation may need clarification.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions