Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mkvlrn opened this issue Feb 3, 2025 · 5 comments
Labels
dotenv Issues and PRs related to .env file parsing watch-mode Issues and PRs related to watch mode

Comments

@mkvlrn
Copy link

mkvlrn commented Feb 3, 2025

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.

@mkvlrn mkvlrn 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 Feb 3, 2025
@mkvlrn mkvlrn 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 Feb 3, 2025
@juanarbol
Copy link
Member

Interesting...

@juanarbol juanarbol added watch-mode Issues and PRs related to watch mode dotenv Issues and PRs related to .env file parsing labels Feb 3, 2025
juanarbol added a commit to juanarbol/node that referenced this issue Feb 3, 2025
As `--env-file` is experimental, this must be marked as experimental as
well.

Refs: nodejs#56887
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
@juanarbol
Copy link
Member

juanarbol commented Feb 3, 2025

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

@mkvlrn
Copy link
Author

mkvlrn commented Feb 3, 2025

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!

@juanarbol
Copy link
Member

Please be careful :)

nodejs-github-bot pushed a commit that referenced this issue Feb 5, 2025
As `--env-file` is experimental, this must be marked as experimental as
well.

Refs: #56887
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
PR-URL: #56893
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
targos pushed a commit that referenced this issue Feb 5, 2025
As `--env-file` is experimental, this must be marked as experimental as
well.

Refs: #56887
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
PR-URL: #56893
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
aduh95 pushed a commit to aduh95/node that referenced this issue Feb 10, 2025
As `--env-file` is experimental, this must be marked as experimental as
well.

Refs: nodejs#56887
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
PR-URL: nodejs#56893
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
@aduh95
Copy link
Contributor

aduh95 commented Feb 14, 2025

@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
Labels
dotenv Issues and PRs related to .env file parsing watch-mode Issues and PRs related to watch mode
Projects
None yet
Development

No branches or pull requests

3 participants