Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

variable not set - returning undefined #124

Closed
DaveyD opened this issue Jul 6, 2017 · 6 comments
Closed

variable not set - returning undefined #124

DaveyD opened this issue Jul 6, 2017 · 6 comments

Comments

@DaveyD
Copy link

DaveyD commented Jul 6, 2017

Hi there
I am trying to get this to work but I am not being successful
I have tried several commands, the simplest of which is:

  "scripts": {
     "test": "cross-env NODE_ENV=test webpack-dev-server",
  },

And when I console.log(process.env.NODE_ENV), I get undefined.

What I originally wanted to do was the following:

  "scripts": {
    "start": "cross-env NODE_ENV=DEV run-p webpack jsonServer",
    "webpack": "webpack-dev-server",
    "jsonServer": "json-server -p 8080 --watch app/data/db.json"
  },

But I thought that maybe it was a problem with run-p (which is part of npm-run-all package), so I tried the simpler version that I posted above, but no matter what I try, I get undefined.

What am I doing wrong?

Thanks,
David

@kentcdodds
Copy link
Owner

Sorry, I'm not sure what's up, that should work...

@DaveyD
Copy link
Author

DaveyD commented Jul 6, 2017

Thanks for the quick reply
Is there any way I can debug this?

@kentcdodds
Copy link
Owner

I would start by going into your node_modules directory and adding console.log statements throughout the cross-env code

@DaveyD
Copy link
Author

DaveyD commented Jul 6, 2017

Just did what you mentioned...
Everything seems to be perfect in your code. The variable is being added to envSetters (as I was able to see with a console log).
I'm not sure why it's not working.

Actually - I just tried logging any kind to environment variable, and all return undefined. TEMP, windir etc, the all returned undefined.
It must be I am not understanding this correctly.

I have a react app which is being run by webpack.
In webpack.config, I set the entry file to be app/index.js
Where are these environment vars supposed to be visible?
I tried in index.js as well as other react component files, and in all of them the process.env is empty

Am I not understanding something correctly?

Please let me know
Thanks,
David

@kentcdodds
Copy link
Owner

Oh, no, you wont find environment variables available in your client-side bundle. Only in node. If you want to expose environment variables in the client, then you can either use this babel plugin or webpack's define plugin. You'll want to do that in addition to using cross-env.

I'll close this issue because I'm 99% sure that's the problem. Good luck!

@DaveyD
Copy link
Author

DaveyD commented Jul 6, 2017

Awesome! Works perfectly!
Thanks so much for clarifying the confusion I was having!

David

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

No branches or pull requests

2 participants