0

I am trying to use the NODE_DEV environment variable within my javascript application run specific code conditionally. I therefor set variable inside my script definition, which I later use like this: npm run dev.

However my function isDevelopment() always returns false, though outputting process.env.NODE_ENV correctly returns development. I have no idea what is wrong with my comparsion. I have tried all kinds of combination, like ', ", ==, === without luck.

What am I doing wrong here?

package.json

{
  // ...
  "scripts": {
    // ...
    "dev": "set NODE_ENV=development && npx npm-run-all --parallel run_nodemon run_browser-sync",
    // ...
  },
  // ...
}

app.js

function isDevelopment() {
    console.log(process.env.NODE_ENV); // outputs as expected: development

    return process.env.NODE_ENV === "development";
}

console.log(isDevelopment()); // outputs: false
Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89
  • try NODE_ENV=development npx npm-run-all --parallel run_nodemon run_browser-sync – cnexans Nov 22 '19 at 12:46
  • @cnexans, thanks for the quick reply. Tried that as well, doesn't change a thing. I am on Windows btw. – Matthias Güntert Nov 22 '19 at 12:47
  • 2
    try process.env.NODE_ENV.trim() to make sure there isn't trailing spaces which won't show in in console.log – John Nov 22 '19 at 12:49
  • The environment variable does get set correctly. – Matthias Güntert Nov 22 '19 at 12:49
  • Calling `trim()` did the trick, so I still don't understand why. Even when setting my env var like this: `"dev": "set NODE_ENV=\"development\" && npx npm-run-all --parallel run_nodemon run_browser-sync",`. Crazy – Matthias Güntert Nov 22 '19 at 12:53
  • 1
    Look like it's a Windows-specific issue: https://stackoverflow.com/a/35176569/4494577 https://stackoverflow.com/questions/39529870/ifprocess-env-node-env-production-always-false – jannis Nov 22 '19 at 12:59
  • Does this answer your question? [if(process.env.NODE\_ENV === 'production') always false](https://stackoverflow.com/questions/39529870/ifprocess-env-node-env-production-always-false) – jannis Nov 22 '19 at 13:08

0 Answers0