would be cool if we could have a similar feature like https://github.com/jeffijoe/keyblade provides for https://github.com/jeffijoe/yenv- throwing when an undefined system variable is accessed.
I don't think it's possible with dotenv. The env variables are accessed from the normal process.env object so we don't have the control to throw an error
you can use a proxy to trap any access to a property on that object.
I know it's not possible currently. This is a feature request.
Ah! So it is possible. Cool idea and really cool approach!
But this behavior seems orthogonal to the purpose of dotenv (loading variables from a file), i.e. you could make a package that does this independent of dotenv and it could be used with or without dotenv.
For that reason, and since the maintainer likes to keep the core very lean, I think it might be better as a separate package.
sorry, I don't 100% understand what you're asking for. can you provide an example?
As I understand it:
.env
DB_HSOT=localhost
main.js
require('dotenv').config()
const host = process.env.DB_HOST
// The proposal is that the above line throws an error since DB_HOST does not exist
@zenflow exactly right. It would mean that dotenv would require proxies to work so you'd have to bump required node version to 6 and newer.
check out the bottom of the README. there are existing modules built on top of dotenv that provide this functionality
no there are not. Explicitly enumerating required variables is not the same as this. No worries, I'll just use yenv instead of dotenv 馃槑
Most helpful comment
you can use a proxy to trap any access to a property on that object.
I know it's not possible currently. This is a feature request.