motdotla / dotenv

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

Automatically use correct `.env` file from `NODE_ENV` #841

Closed jzaynal closed 3 months ago

jzaynal commented 3 months ago

This may well be a me issue as I'm not familiar with this package enough, so please let me know if that's the case.

I would expect dotenv to automatically pick up correct .env file based on value of NODE_ENV.

For example, if I have .env and .env.test and do NODE_ENV=test node, running require('dotenv').config() without specifying path explicitly I would expect it to pull values from .env.test.

If I have to specify path: '.env.' + process.env.NODE_ENV I'm sorry but this defeats the whole point of multi-environment configuration with the library.

motdotla commented 3 months ago

dotenv has never done that. you might find dotenvx useful though. it is new and has a close analog to what you are describing.

https://github.com/dotenvx/dotenvx

jzaynal commented 3 months ago

@motdotla I understand that it hasn't ever done that but do you think my expectation is reasonable? i.e. do you think it should do that? Reason I'm asking is you must've come across the need for this behaviour in the lifetime of dotenv and now dotenvx - I see that it's simpler to configure different environments with dotenvx now but still doesn't just automatically use it from NODE_ENV environment variable; I still have to specify explicitly.

If you have indeed considered it, can you explain the drawbacks of this as I'm now using a 'customised' dotenv configuration loading that does just that and I want to be careful if there are pitfalls.

motdotla commented 3 months ago

yes, i understand. it's somewhat reasonable but not given the scale of dotenv at this point. dotenv is depended on by major libraries, frameworks, and platforms. changing this would be a massive surprise and disruption.

that's why it lives in dotenvx.