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

Node is casting all env vars to Strings #12126

Closed
daviddias opened this issue Mar 29, 2017 · 11 comments
Closed

Node is casting all env vars to Strings #12126

daviddias opened this issue Mar 29, 2017 · 11 comments
Labels
doc Issues and PRs related to the documentations. question Issues that look for answers.

Comments

@daviddias
Copy link

  • Version: v6.10.0

  • Platform: Mac OS X

  • Subsystem:

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 :)

@sam-github
Copy link
Contributor

Its that, or throw an error.... all env vars ARE strings, by definition.

@sam-github sam-github added the question Issues that look for answers. label Mar 29, 2017
@evanlucas
Copy link
Contributor

I know this has come up a few times. Should we perhaps document that values are coerced to strings?

@ORESoftware
Copy link
Contributor

ORESoftware commented Mar 30, 2017

@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.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 30, 2017

@evanlucas I think it could be beneficial for beginners. We hint around the idea in the docs currently.

@ORESoftware
Copy link
Contributor

ORESoftware commented Mar 30, 2017

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.

@addaleax
Copy link
Member

@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.

@ORESoftware
Copy link
Contributor

ORESoftware commented Mar 30, 2017

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.

@TimothyGu
Copy link
Member

TimothyGu commented Mar 30, 2017

@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.

@TimothyGu TimothyGu reopened this Mar 30, 2017
@jasnell
Copy link
Member

jasnell commented Mar 30, 2017

Having some additional documentation around this would not hurt at all.

@TimothyGu TimothyGu added the doc Issues and PRs related to the documentations. label Mar 30, 2017
@aqrln
Copy link
Contributor

aqrln commented Mar 30, 2017

@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 😄

8ff9b56 (#4924)

@daviddias
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

9 participants