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);
Closing this for https://github.com/gatsbyjs/gatsby/issues/660
For reference, this issue is related to https://github.com/gatsbyjs/gatsby/issues/337#issuecomment-226956766
We autoset
NODE_ENV=productionongatsby 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
Most helpful comment
We work around this in
gatsby-config.jsby using a secondary variable calledACTIVE_ENVIn our CI pipeline we set
ACTIVE_ENVand the appropriate file is loaded. Locally we work out of.env.developmentor if we want to test prod configs we can do this viaACTIVE_ENV=production gatsby develop