Wednesday, 3 May 2017

Brunch expose NODE_ENV or environment variable to javascript

I am writing an npm script that will execute a brunch build. I want to pass along an environment variable so that the app will know which set of URLs to use by default for this build (the set of URLs will be changeable at user request from UI). Here's my npm scripts setup so far (I'm on Windows):

"scripts": {
  "build:localhost": "SET NODE_ENV=localhost && brunch build",
  "build:develop": "SET NODE_ENV=develop && brunch build",
  "build:production": "SET NODE_ENV=production && brunch build"
}

If I npm run build:localhost and console.log(process.env.NODE_ENV); in my brunch-config.js, I get localhost as I would expect. But the same console.log(process.env.NODE_ENV); in one of my javascript files being compiled returns development. That's not what I expect - I want it to be localhost.

How do I pass the NODE_ENV variable that I have set through to my javascript code? More generally, how could I supply a variable to my javascript code from brunch? The --env flag for the brunch command seems to just be about which setup is used in the overrides block of the brunch config.



via Scott H

No comments:

Post a Comment