-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Environment variables are not captured utilizing NODE_ENV in Webpack #10563
Comments
So the problem is how to set environment variables for staging builds? |
Correct. Basically we can’t use NODE_ENV because that gets set by the build |
But we can't use |
Not really as webpack 4.x introduced the See https://webpack.js.org/guides/production/#specify-the-mode Internally the mode for webpack builds can be changed / set depending on the setup / script |
Ah cool. Yeah setting NODE_ENV seems ideal as it's widely used for this sort of thing. |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open! Thanks for being a part of the Gatsby community! 💪💜 |
Hey again! It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m Thanks again for being part of the Gatsby community! |
Any updates on this? I am running into an issue where I have multiple environments and only one needs to have the Google Tag Manager injected. I am using the gatsby-google-tagmanager plugin and every time i run |
Description
Environment variables (captured in
.env.ENVIRONMENT
, e.g..env.staging
) do not get exposed correctly to the code that gets bundled with Webpack, e.g. the client-side, non-Node code.We can see this by walking through the build process:
Add a script,
build:staging
gatsby build
is kicked off via the CLI hereEnvironment variables are sourced via Webpack here
Steps to reproduce
yarn
ornpm install
yarn build:staging
API_KEY
in.env.staging
should be present in the built codeExpected result
The environment variable
API_KEY
from.env.staging
should be present inpublic/index.html
Actual result
The environment variable
API_KEY
from.env.production
is used.The Fix
I don't believe we can overwrite
NODE_ENV
asproduction
is checked fairly often in various plugins/APIs/etc. Usingprocess.env.NODE_ENV = process.env.NODE_ENV || 'production'
ingatsby-cli
seems to break the build.As such - I think there are two things that we should do here to remediate the issue.
NODE_ENV
ACTIVE_ENV
as we document hereI don't love the indirection and extra steps here--but not sure I see a better solution off hand. Opinions more than welcome here!
cc @xjamundx
The text was updated successfully, but these errors were encountered: