motdotla / dotenv

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

Using an array for config.path does not work #796

Closed tran-simon closed 7 months ago

tran-simon commented 7 months ago

According to the readme, the path option should accept an array

path Default: path.resolve(process.cwd(), '.env') By default, config will look for a file called .env in the current working directory. Pass in multiple files as an array, and they will be loaded in order. The first value set for a variable will win. require('dotenv').config({ path: ['.env.local', '.env'] })

It does not seem to be working properly

In this snippet, process.env.TEST is undefined when using path: [".env"]

console.log("Loading with array");
require("dotenv").config({ path: [".env"] });
console.log(process.env.TEST);

console.log("Loading with path");
require("dotenv").config({ path: ".env" });

console.log(process.env.TEST);

Output:

Loading with array undefined Loading with path test

SANDBOX

motdotla commented 7 months ago

tests should have caught this, which means a bad test. it's using mocking - always brittle. i'm rearranging the tests to confirm and then will check out your pr. thank you.