motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.04k stars 854 forks source link

config() accetps any path #768

Closed ShahriarKh closed 1 year ago

ShahriarKh commented 1 year ago

Config path accepts any string. I tried:

dotenv.config({ path: 'random-file-name.jpg' })

and the code executed without any errors!

Currently, I'm using this hacky way to ensure that correct env file is loaded:

if (!dotenv.config({ path: '.env.local' }).parsed) {
  // throw an error or something...
}

But I believe the path should do some checking to only allow .env.* files and throw an error if the file is invalid.

👀 I think this PR is related: #651

motdotla commented 1 year ago

We choose not to throw an error because in production the .env file won't be there.

If you'd like to throw an error, you can do a file check first using node's fs lib. then pass to dotenv.