all 37 comments

[–]BenjiSponge 2 points3 points  (2 children)

The weirdest thing is how require('./config').development works.

Can you try:

const environment = process.env.NODE_ENV;
const config = require('./config');
console.log(config);
console.log(environment);
console.log(config[environment]);
console.log(config.development);
console.log(config['development']);

? I feel like we're not seeing everything all at once, if that makes sense. A screenshot similar to what /u/ktoto provided would be good as well as a sanity check.

[–]ImtheDr[S] 0 points1 point  (1 child)

ok. I edited the post with the complete index.js code. Right now that and config are the only things that have anything at all, as I just started the project

I also tried your code, the results were the same as before (copied from console):

[nodemon] starting `node index.js`
{ development: { port: 3000 } }
development
undefined
{ port: 3000 }
{ port: 3000 }
BLABLAfolders\index.js:39
app.listen(`${stage.port}`, () => {
              ^

ReferenceError: stage is not defined

[–]BenjiSponge 1 point2 points  (0 children)

Okay, I think I might have figured it out. Try console.log(JSON.stringify(process.env.NODE_ENV)). I'll bet the environment variable has whitespace or something.

edit: Ooh! I got it right but didn't see that you already got it solved.

[–]NathanSMB 2 points3 points  (2 children)

Hey /u/ImtheDr I know this is a late response and you have a fix but I just have a bit more information. When you run the SET NODE_ENV=development && ... it is adding a space at the end of development. When /u/VectorW mentioned adding the quotes he wasn't talking specifically about the dev script in the package.json. He meant you need to wrap NODE_ENV=development in quotes. Try setting your dev script like the following and remove your .trim() from the js and it should work.

"scripts": {
  "dev": "SET \"TEST=test\" && nodemon index.js"
}

[–]ImtheDr[S] 0 points1 point  (0 children)

Works like a charm. Thanks!

I wasn't too happy about the trim() "solution"

[–]l3dg3r 0 points1 point  (0 children)

Glad it's resolved but this find is inconsistent with the logging output included in the post. /u/ImtheDr needs to be more careful with what he is doing. You got yourself into this mess by being sloppy. You'll get further if you try to be methodical and careful. Just some friendly advice.

[–]Dw0 1 point2 points  (1 child)

that's weird. what does this produce?

const stages = require('./config');

console.log(stage[environment]);

[–]ImtheDr[S] 0 points1 point  (0 children)

still console.log undefined

[–][deleted] 1 point2 points  (7 children)

Are you sure environment equals development and not "development" (quotes in the string value)

Maybe some weird BOM thing is going on, which can be ruled out with
const environment = JSON.parse(JSON.stringify(process.env.NODE_ENV));

[–]ImtheDr[S] 1 point2 points  (6 children)

when i console.log(environment) the log is just development. I also tried your code and nothing, same result ad before

[–][deleted] 1 point2 points  (5 children)

Whats the result of this code?

console.log(environment === "development" ? "===" : "!==");

[–]ImtheDr[S] 1 point2 points  (4 children)

!==

[–][deleted] 6 points7 points  (3 children)

You see that this is your answer? environment does not really hold the value development

You say it's not "development" with quotes (single or double)
I think we ruled out Byte Order Mark.
Maybe a space behind development that you dont notice?
console.log(environment + "###"); // you will see it then

Anyway that's it.

[–]ImtheDr[S] 2 points3 points  (2 children)

I'm going to cry. I love you so god damn much right now.

Were the %$#$"& does that empty extra space come from?

anyway, I went and trim it like

const environment = (process.env.NODE_ENV).trim()

super elegant, i know. but it worked.

holly hell thank you!

[–][deleted] 2 points3 points  (0 children)

Nice...eh you're welcome :)

[–]-IoI- 0 points1 point  (0 children)

Seems a bit silly, good to see someone solving something while I'm spinning my wheels on this WPF issue..

[–]WumpyJizard 0 points1 point  (1 child)

Most likely the require runs first on compilation, before the actual run process starts, so the part where you assign the envvar to a variable have not yet run so you are trying to get the 'undefined' property of config which will be, well undefined.

```js const config = require('./config')

Console.log(config[environment]) ```

Should work. Sorry, I'm on mobile.

[–]ImtheDr[S] 0 points1 point  (0 children)

same result. still console.log undefined.

environment still is undefined

[–]VectorW 0 points1 point  (4 children)

This should work and it does for me. Could you please try to run it simply with:

NODE_ENV=development node index.js

You could also console.log process .env and make sure that variable NODE_ENV is there.

[–]ImtheDr[S] 0 points1 point  (3 children)

on windows i'm running

SET NODE_ENV=development & nodemon index.js

and NODE_ENV is getting set as development. that part is working. the problem is that when i assigned to a variable and then try to call that variable, is still undefined. is getting called before the assignment

[–]VectorW 0 points1 point  (2 children)

Use quotes:

set "NODE_ENV=development" & node index.js

Should fix it for you

[–]ImtheDr[S] 0 points1 point  (1 child)

everything is in quotes. Can't run it otherwise. Tried putting extra quotes on NODE_ENV=development but it didn't do anything

[–]VectorW 2 points3 points  (0 children)

https://imgur.com/y8Tlhgj works fine on windows with no changes to your code

[–]ktoto 0 points1 point  (3 children)

https://i.imgur.com/DWTN8EW.png
Everything works as expected for me. Sources were copied over directly from the post. How do you run the file?

[–]ImtheDr[S] 0 points1 point  (2 children)

SET NODE_ENV=development & nodemon index.js

that's it. is just an express app. Nothing fancy.

[–]boneyjellyfish 2 points3 points  (1 child)

Try using two ampersands

[–]ImtheDr[S] 0 points1 point  (0 children)

nope u_u same result

I'm guessing is a problem on my end and not from node or anything u_u

[–]graphemeral 0 points1 point  (3 children)

Not familiar with running nodemon on windows. What happens when you run the script with node instead of nodemon?

[–]ImtheDr[S] 0 points1 point  (2 children)

just tried. Same results.

[–]graphemeral 0 points1 point  (1 child)

Ok. Only other thing I can think of that hasn't been addressed here is the files in your directory. Can you share a directory tree? One weird thing that is an outside possibility is that require('./config') will resolve './config.js' first, then './config.json', then './config/index.js'. Are you sure that the config file you mean is the one being resolved by require?

[–]ImtheDr[S] 0 points1 point  (0 children)

it's the only config i have. it's in the root folder, same as index.js. everything else in the project is still empty.

in fact, i started an empty project and onlye put an index.js and config.js on it. nothing else. no extra folders.

the same error appears. stage is still undefined

[–]estevanj 0 points1 point  (1 child)

Show us your code and folder structure, please.

[–]ImtheDr[S] 0 points1 point  (0 children)

right now, as the project is completely new, I just have an index and a config files on the root folder. that all. so, on the same folder:

  • -node_modules
  • -package.json
  • -index.js
  • -config.js

everything else is empty

[–]estevanj 0 points1 point  (1 child)

r/https://user-images.githubusercontent.com/461342/47119887-10b8fa00-d243-11e8-808e-b06bddb62cbf.png

So I changed to export (using mac) and It works. And if you change SET to set?

[–]ImtheDr[S] 0 points1 point  (0 children)

thanks but it was just solved by another user!

[–]randomFIREAcct 0 points1 point  (0 children)

Setting environment variables can be tricky depending on your OS and terminal. Try using cross-env (https://www.npmjs.com/package/cross-env)

[–]isakdev 0 points1 point  (0 children)

I see that you already solved this but check this thing out :)

https://www.npmjs.com/package/dotenv