Skip to content

--env-file-if-exists throws error when the .env file doesn't exist and when combined with --watch #56887

Description

@mkvlrn

Version

23.7.0

Platform

Linux fedora 6.12.11-200.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jan 24 04:59:58 UTC 2025 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Run any script in a project where a .env file doesn't exist:

node --env-file-if-exists=.env --watch main.js

If the .env file exists or you don't use the --watch flag, script runs normally without throwing errors.

How often does it reproduce? Is there a required condition?

These need to be true for the problem to occur:

  • use the flag --env-file-if-exists pointing to a non existing .env file, --env-file-if-exists=.env
  • use the flag --watch

What is the expected behavior? Why is that the expected behavior?

Script should run normally with the following message being printed right before it:

.env not found. Continuing without it.

What do you see instead?

An error stating .env was not found.

.env not found. Continuing without it.
node:internal/fs/watchers:247
    const error = new UVException({
                  ^

Error: ENOENT: no such file or directory, watch '/home/mkvlrn/github/template-discordbot/.env'
    at FSWatcher.<computed> (node:internal/fs/watchers:247:19)
    at watch (node:fs:2551:36)
    at FilesWatcher.watchPath (node:internal/watch_mode/files_watcher:109:21)
    at FilesWatcher.filterFile (node:internal/watch_mode/files_watcher:128:12)
    at start (node:internal/main/watch_mode:78:13)
    at init (node:internal/main/watch_mode:148:15)
    at node:internal/main/watch_mode:160:1 {
  errno: -2,
  syscall: 'watch',
  code: 'ENOENT',
  path: '/home/mkvlrn/github/template-discordbot/.env',
  filename: '/home/mkvlrn/github/template-discordbot/.env'
}

Node.js v23.7.0
.env not found. Continuing without it.
node:events:485                                                                                       
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at target._send (node:internal/child_process:877:20)
    at target.send (node:internal/child_process:751:19)
    at #createModuleJob (node:internal/modules/esm/loader:552:15)
    at #getJobFromResolveResult (node:internal/modules/esm/loader:312:34)
    at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:280:41)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:582:25)
Emitted 'error' event on process instance at:
    at node:internal/child_process:881:39
    at process.processTicksAndRejections (node:internal/process/task_queues:85:11) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Node.js v23.7.0

Additional information

Neither --watch nor --env-file /--env-file-if-exists documentations warn about not combining these flags.

Activity

  1. changed the title [-]--env-file-if-exists throws error when the .env file doesn't exist if paired with --watch[/-] [+]--env-file-if-exists throws error when the .env file doesn't exist when combined with --watch[/+] on Feb 3, 2025
  2. changed the title [-]--env-file-if-exists throws error when the .env file doesn't exist when combined with --watch[/-] [+]--env-file-if-exists throws error when the .env file doesn't exist and when combined with --watch[/+] on Feb 3, 2025
  3. juanarbol commented on Feb 3, 2025

    @juanarbol
    Member

    Interesting...

  4. added
    watch-modeIssues and PRs related to watch mode.
    dotenvIssues and PRs related to .env file parsing.
    on Feb 3, 2025
  5. juanarbol commented on Feb 3, 2025

    @juanarbol
    Member

    This seems to be fixed in upstream.

    ./node --env-file-if-exists=.env --watch main.js
    .env not found. Continuing without it.
    .env not found. Continuing without it.
    Hola mundo
    Completed running 'main.js'

    Thanks for this report, please make sure you use LTS releases. See https://github.com/nodejs/release?tab=readme-ov-file#release-schedule

    This bug is fixed in v22.13.1.

    $ node -v
    v22.13.1
    
    $ node --env-file-if-exists=.env --watch main
    .env not found. Continuing without it.
    .env not found. Continuing without it.
    Hola mundo
    Completed running 'main'

    I'll proceed to close this issue as it is fixed and released

  6. mkvlrn commented on Feb 3, 2025

    @mkvlrn
    Author

    Thanks for looking into it. I was using latest (23) because of the removal of the --experimental-strip-types flag and never even tried with LTS - that was a terrible oversight of mine. Too excited to run those ts files!

  7. juanarbol commented on Feb 3, 2025

    @juanarbol
    Member

    Please be careful :)

  8. aduh95 commented on Feb 14, 2025

    @aduh95
    Contributor

    @juanarbol rather than pushing users off of Current releases (we want users to use Current releases, it's the whole point of having them), it would more useful to use the report to tag the relevant folks and make sure the bug doesn't make its way into an LTS release – which has happen, see #57040

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotenvIssues and PRs related to .env file parsing.watch-modeIssues and PRs related to watch mode.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions