Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean and Numeric env variables are not casted properly #544

Closed
ajcastro opened this issue Jun 9, 2021 · 2 comments
Closed

Boolean and Numeric env variables are not casted properly #544

ajcastro opened this issue Jun 9, 2021 · 2 comments

Comments

@ajcastro
Copy link

ajcastro commented Jun 9, 2021

I tried IS_BOOL=true but when I console.log(process.env.IS_BOOL); it displays a string "true" but it should be casted to boolean true.

I also tried IS_NUM=10 and it is a string "10" but it should be numeric 10.
I also tried IS_FLOAT=10.5 and it is a string "10.5" but it should be a float 10.5.

@ajcastro ajcastro changed the title Boolean env variables are not casted to boolean Boolean and Numeric env variables are not casted properly Jun 9, 2021
@zenflow
Copy link

zenflow commented Jun 9, 2021

Environment variables are strings though, not numbers or booleans. That is true independent of this project and even of Node.js.
The function of this library is to load predefined env vars (strings) from a .env file into process.env (collection of strings).

Actually Node.js won't even let you assign any other type of value (besides a string) to a property of process.env, since that value will be converted to a string:
image

Refer to the other similar issues for discussion of ways to deal with casting env vars to other (not string) value types: #51 #77
There are some robust solutions offered, but the simple solution is just code that casts/parses each string according to its intended type:

const IS_BOOL = process.env.IS_BOOL === "true";
const IS_INT = parseInt(process.env.IS_INT, 10);
const IS_FLOAT = parseFloat(process.env.IS_FLOAT);

@ajcastro
Copy link
Author

ajcastro commented Jun 10, 2021

Thanks for the response. I came from Laravel/PHP where variables are parsed by default, so I thought it was the same. Someone from the quasar community answered that I can use this https://github.com/niftylettuce/dotenv-parse-variables to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants