Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to change NODE_ENV manually #617

Closed
oliverbenns opened this issue Dec 28, 2016 · 8 comments
Closed

Unable to change NODE_ENV manually #617

oliverbenns opened this issue Dec 28, 2016 · 8 comments

Comments

@oliverbenns
Copy link
Contributor

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"

@KyleAMathews
Copy link
Contributor

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

@oliverbenns
Copy link
Contributor Author

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.

@oliverbenns
Copy link
Contributor Author

oliverbenns commented Feb 15, 2017

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);

@oliverbenns
Copy link
Contributor Author

oliverbenns commented Feb 15, 2017

Closing this for #660

@yvele
Copy link

yvele commented Oct 30, 2017

For reference, this issue is related to #337 (comment)

We autoset NODE_ENV=production on gatsby build

@anaibol
Copy link
Contributor

anaibol commented Dec 18, 2017

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

@jongear
Copy link
Contributor

jongear commented Apr 6, 2018

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

@ruslan-mystore
Copy link

@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

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

No branches or pull requests

6 participants