Version: v6.10.0
Platform: Mac OS X
I might have just realised this now, but is it intended that all env vars are casted to Strings by default? See:
禄 node
> process.env.BANANAS = 3
3
> typeof process.env.BANANAS
'string'
> process.env.BANANAS = undefined
undefined
> typeof process.env.BANANAS
'string'
>
Thank you :)
Its that, or throw an error.... all env vars ARE strings, by definition.
I know this has come up a few times. Should we perhaps document that values are coerced to strings?
@evanlucas all env vars are strings my man. if you want to use different types in your env variables, might I suggest JSON?
LUCAS_VAR={"number":3, "boolean":true} node lucas.js
in lucas.js you can do:
let lucas = JSON.parse(process.env.LUCAS_VAR);
typeof lucas.number -> number
typeof lucas.boolean -> boolean
I have used this pattern to great effect and you can too.
As you might understand, this is the same kind of thing a querystrings in URLs, they are always strings. You can use the same pattern to include JSON in query strings.
@evanlucas I think it could be beneficial for beginners. We hint around the idea in the docs currently.
I don't see why it's necessary to document this. It's the default behavior for any language tmk. But maybe it wouldn't hurt.
@ORESoftware Because process.env looks like a normal JavaScript object in every way but behaves very differently once you try to change values. I can see why that would throw off people.
just want to emphasize that process.env is a normal JS object, not just that it looks like one :) I know you know that (duh) but just idk. process.env just happens to have all strings as values. IDK, seems very intuitive to me that it's only strings, this is one of the things that fills up docs but doesn't really help anyone IMO.
@ORESoftware, it is not a normal object. In addition to casting values to string, Node.js v8 will not allow keys to be symbols:
> process.env[Symbol.iterator] = 2
TypeError: Cannot convert a Symbol value to a string
at repl:1:30
These behaviors all differ from standard JS object semantics (edit: in other words, process.env is an exotic object), and I agree with @addaleax to document this somehow.
Having some additional documentation around this would not hurt at all.
@evanlucas
I know this has come up a few times. Should we perhaps document that values are coerced to strings?
I guess you have already documented that 馃槃
https://github.com/nodejs/node/commit/8ff9b56c92d36ad8bdfd2d9be3a535e61315fb38 (https://github.com/nodejs/node/pull/4924)
Thank you all for the quick answers :) Fully understand now. I was familiar with the fact that env vars are strings in every other language, I was just not expecting that node would coerce vars to string on runtime as well.
Seems that docs are updated too, all good. Thanks!
Most helpful comment
Thank you all for the quick answers :) Fully understand now. I was familiar with the fact that env vars are strings in every other language, I was just not expecting that node would coerce vars to string on runtime as well.
Seems that docs are updated too, all good. Thanks!