Dotenv: [Suggestion] Support primitive types in .env values by using comment.

Created on 5 Feb 2018  ·  5Comments  ·  Source: motdotla/dotenv

Thanks for such useful package! ❤️

Currently this package injects env vars as string, but it is not always string that we want, for example if something like

# my .env file
ENABLE_SOMETHING=true

appears in my config, the if (process.env.ENABLE_SOMETHING) judgement is broken and we have to either use if (process.env.ENABLE_SOMETHING === 'true') or write another lib to set and convert that variable.

Suppose we have a convention:we can use comment to mark the type of the variable (or not, and it will still be a string). Take the last example:

# my .env file
ENABLE_SOMETHING=true # boolean

and process.env.ENABLE_SOMETHING === true is now working。You can still use other programming language or other approach to inject your env vars, for example:

$ source ./.env

will still be working.

Most helpful comment

@t1ger-0527 Please update if you make a package for this!

All 5 comments

Strings are truthy-falsy

FOO=1   #truthy
BAR=    #falsy

Then you can do if (process.env.FOO) {} else {} and such

Thanks for the solution. @zenflow
But what about numbers?

Is this suggestion a bad idea? Or should I make a PR?

But what about numbers?

const port = Number(process.env.PORT)

Is this suggestion a bad idea? Or should I make a PR?

I'm not sure if it is a bad idea.. I think that the benefit may not outweigh the added complexity. For example, what if I have a variable that I intend to be a string but it looks like a boolean or number? That said, I think it would be nice to have if it could work.

Also, as an FYI, I know the maintainers of this repo like to keep the core (very) small and simple. They may suggest you publish a new package like dotenv-typecast.

Ah.. I missed this:

Suppose we have a convention:we can use comment to mark the type of the variable (or not, and it will still be a string). Take the last example:

You already had a solution to that problem.

Yeah I don't see any real problems with this, and I think it would be convenient 👍 .. One suggestion though: maybe denote the type with something other than # so that they are not easily confused with actual for-humans-only comments.

But I might hold off on a PR because of the FYI thing

@t1ger-0527 Please update if you make a package for this!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndersDJohnson picture AndersDJohnson  ·  4Comments

shellscape picture shellscape  ·  3Comments

awesomejerry picture awesomejerry  ·  4Comments

DesignByOnyx picture DesignByOnyx  ·  4Comments

datasmurfen picture datasmurfen  ·  5Comments