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

process.env does not expose its keys as expected #795

Closed
jobi opened this issue Sep 29, 2016 · 7 comments
Closed

process.env does not expose its keys as expected #795

jobi opened this issue Sep 29, 2016 · 7 comments
Milestone

Comments

@jobi
Copy link

jobi commented Sep 29, 2016

I'm not sure if this is expected, but it doesn't feel right.

If I run an app with

$ REACT_APP_FOO=bar npm start

I can access process.env.REACT_APP_FOO, but REACT_APP_FOO in process.env returns false, and process.env["REACT_APP_FOO"] is undefined.

I'm trying to have something like:

function getConfig(key) {
   if (key in process.env) {
     return process.env[key];
   }

   return defaults[key];
}
@tbillington
Copy link

tbillington commented Sep 29, 2016

If it's not there it will be undefined, your could write it like this.

function getConfig(key) {
   return process.env[key] || defaults[key];
}

@gaearon
Copy link
Contributor

gaearon commented Sep 29, 2016

I think this might be possible to fix if we use

new DefinePlugin({
  'process.env': {
    NODE_ENV: ...,
    OTHER_VAR: ...,
    // ...
  }
})

form instead of

new DefinePlugin({
  'process.env.NODE_ENV': ...,
  'process.env.OTHER_VAR': ...,
  // ...
})

like we do now.

Would you like to give it a try and maybe submit a PR?

The relevant code is in packages/react-scripts/config/env.js.
(It gets imported from webpack configs in packages/react-scripts/config.)

The contributing instructions are here.

@thien-do
Copy link
Contributor

Hey @gaearon I can do this.

@thien-do
Copy link
Contributor

Please check the PR above. I hope it is what you explained

@gaearon
Copy link
Contributor

gaearon commented Sep 30, 2016

Fixed in #807.

@gaearon gaearon closed this as completed Sep 30, 2016
@gaearon gaearon added this to the 0.7.0 milestone Sep 30, 2016
@mars
Copy link
Contributor

mars commented Oct 12, 2016

This is amazing to find already merged!

I've been working on a solution for the env vars issue on the Heroku buildpack. The ability to enumerate process.env entries will directly contribute to that solution 😄🍻

@fson
Copy link
Contributor

fson commented Oct 12, 2016

This is going to be included in 0.7.0, which will be out soon.

@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants