Gatsby: Unable to change NODE_ENV manually

Created on 28 Dec 2016  Â·  8Comments  Â·  Source: gatsbyjs/gatsby

Looking at this PR, it looks like you should be able to edit the NODE_ENV. This works for develop but the build command always overwrites to "production"

Testing with NODE_ENV=foo npm run build. With the package json script being the default "build": "gatsby build"

Most helpful comment

We work around this in gatsby-config.js by using a secondary variable called ACTIVE_ENV

// gatsby-config.js

let activeEnv = process.env.ACTIVE_ENV

if (!activeEnv) {
  activeEnv = 'development'
}

require('dotenv').config({
  path: `.env.${activeEnv}`,
})

In our CI pipeline we set ACTIVE_ENV and the appropriate file is loaded. Locally we work out of .env.development or if we want to test prod configs we can do this via

ACTIVE_ENV=production gatsby develop

All 8 comments

Hmmm... that is true, we set somewhere in code NODE_ENV to production during build — but you kinda have to have it that way — there's all sorts of optimizations within React and other modules within the React ecosystem that are only turned on when NODE_ENV=production.

Why not just set another env variable e.g. MY_CUSTOM_VAR=whatever? You have to modify the webpack config slightly to add a new DefinePlugin like in core

Yeah I also tried custom variables though bash but logging process.env only gave me NODE_ENV. I'll have a look at the webpack config.

Any news on this? Can't seem to set any custom variables, e.g.: "develop": "TEST=foobarbaz gatsby develop",.

For those in a similar circumstance, my current workaround is to create a environment js file that changes depending on environment (in Gatsby's case, build task).

const env = {
  production: {
    analyticsId: 'UA-11111111-1',
    api: {
      url: 'https://api.google.com',
    },
  },
  development: {
    analyticsId: 'UA-00000000-0',
    api: {
      url: 'http://localhost:3000'
    },
  },
};

export default env[process.env.NODE_ENV] || {};

import env from 'env';

console.log(env.api.url);

For reference, this issue is related to https://github.com/gatsbyjs/gatsby/issues/337#issuecomment-226956766

We autoset NODE_ENV=production on gatsby build

So we can't override NODE_ENV? I wanted to set NODE_ENV to staging so gatbsy uses .env.staging env variables file

We work around this in gatsby-config.js by using a secondary variable called ACTIVE_ENV

// gatsby-config.js

let activeEnv = process.env.ACTIVE_ENV

if (!activeEnv) {
  activeEnv = 'development'
}

require('dotenv').config({
  path: `.env.${activeEnv}`,
})

In our CI pipeline we set ACTIVE_ENV and the appropriate file is loaded. Locally we work out of .env.development or if we want to test prod configs we can do this via

ACTIVE_ENV=production gatsby develop

@jongear i get this error according to your workaround (same as in gatsby docs) and tried to install dependencies , but without succes:

 Failed to compile with 1 errors                                                                                                              18:26:15

This dependency was not found:

* fs in ./~/dotenv/lib/main.js

To install it, you can run: npm install --save fs
Was this page helpful?
0 / 5 - 0 ratings

Related issues

benstr picture benstr  Â·  3Comments

brandonmp picture brandonmp  Â·  3Comments

andykais picture andykais  Â·  3Comments

ferMartz picture ferMartz  Â·  3Comments

timbrandin picture timbrandin  Â·  3Comments