Skip to content

comments in .env files are parsed as values #52759

@MarijnMensinga

Description

@MarijnMensinga

Version

20.12.0+, 21.0.0+, 22.0.0

Platform

Darwin XXX.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

# .env

TEST_VAR=OK
# TEST_VAR=WRONG1
#TEST_VAR=WRONG2
# .env.local

#TEST_VAR=WRONG3
# index.js

console.log(process.env.TEST_VAR)

Run with single env file:
nvm run 20.6.0 --env-file=.env index.js => OK
nvm run 20.11.1 --env-file=.env index.js => OK
nvm run 20.12.0 --env-file=.env index.js => WRONG2
nvm run 22.0.0 --env-file=.env index.js => WRONG2

Run with multiple env files:
nvm run 20.11.1 --env-file=.env --env-file=.env.local index.js => OK
nvm run 20.12.0 --env-file=.env --env-file=.env.local index.js => WRONG3
nvm run 22.0.0 --env-file=.env --env-file=.env.local index.js => WRONG3
Note: for simplicity I re-used the .env file, but using different files yields the same behaviour

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

Works as long as --env-file is present

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

I expect only uncomment values in .env files to be parsed as actual values. So the output of the reproduction script should be "OK". This is in-line with every other .env parsing library. Also node v20.6.0 -> v20.11.1 behaves like that:

$ nvm run 20.6.0 --env-file=.env index.js  
Running node v20.6.0 (npm v9.8.1)
OK
$ nvm run 20.11.1 --env-file=.env index.js
Running node v20.11.1 (npm v10.2.4)
OK
$ nvm run 20.11.1 --env-file=.env --env-file=.env.local index.js
Running node v20.11.1 (npm v10.2.4)
OK

What do you see instead?

The last read commented values is outputed instead of the expected value:

$ nvm run 20.12.0 --env-file=.env index.js
Running node v20.12.0 (npm v10.5.0)
WRONG2
$ nvm run 20.12.0 --env-file=.env --env-file=.env.local index.js
Running node v20.12.0 (npm v10.5.0)
WRONG3
$ nvm run 22.0.0 --env-file=.env index.js                      
Running node v22.0.0 (npm v10.5.1)
WRONG2
$ nvm run 22.0.0 --env-file=.env --env-file=.env.local index.js
Running node v22.0.0 (npm v10.5.1)
WRONG3

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions