-
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
Pass GATSBY* environment variables into app #660
Comments
Javascript in Webpack isn't the same as Javascript within Node. So to get an environmental variable from the node context into Webpack, you have to bridge the gap. By default, Gatsby passes in gatsby/lib/utils/webpack.config.js Line 118 in 11405b5
To add additional environmental variables you'll need to use the https://github.com/gatsbyjs/gatsby#how-to-use-your-own-webpack-loaders |
I missed this part of the doc, thank you. create-react-app provides an unified way to define and use environment variables. It is very convenient. Wouldn't it be interesting to have a similar feature with Gatsby ? With Gatsby, these variables (for example, a Google Analytics UA code) have to be added to Sure, there is the |
Oh wow yes! Let's absolutely copy that. I am a big fan of using environmental variables just didn't occur to me that we could make it cleaner. The main thing I'd want to avoid is bringing over all the crap shell default env variables which I'm assuming/hoping CRA does. Could you take this on and add a PR? |
Awesome, glad you like the suggestion ! I'm really busy atm, but hell, I guess it applies to you and everyone here as well. Immense congrats for Gatsby btw, most fantastic blog engine I've ever used ! 👍 :) |
Quick question: In create-react-app, env variables are considered and passed down only if they start with For example, here is how one of my
The value of this is not clear to me though, I'd rather have env variables non-prefixed (their values tend to always stay the same, event if I migrate from one blog engine to an other). Any thoughts on this? In our case, should the env variables be required to be prefixed with |
The reason they did that I'm assuming is that most people's shells are littered with environment variables. E.g. mine looks like Kyles-MacBook-Pro-2:gatsby (master=)$ env
TERM_PROGRAM=iTerm.app
TERM=xterm
SHELL=/bin/bash
TMPDIR=/var/folders/79/hy76bb9j0jlbs3kzr6mz45yc0000gn/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.fG7tdrI8PL/Render
TERM_PROGRAM_VERSION=3.0.12
TERM_SESSION_ID=w0t13p0:DB3B3D19-4479-4715-8F93-0B07D13C4501
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.000s5uoseh/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
PATH=/usr/local/sbin:/usr/local/bin:/var/lib/gems/1.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:/usr/texbin
PWD=/tmp/gatsby
EDITOR=vim
LANG=en_US.UTF-8
ITERM_PROFILE=Default
XPC_FLAGS=0x0
PS1=\[\e[1;36m\]\h:\W\[\e[m\]\[\e[1;32m\]$(__git_ps1 " (%s)")\[\e[m\]\[\e[1;36m\]$ \[\e[m\]
XPC_SERVICE_NAME=0
SHLVL=1
COLORFGBG=15;0
ITERM_SESSION_ID=w0t13p0:DB3B3D19-4479-4715-8F93-0B07D13C4501
DISPLAY=/private/tmp/com.apple.launchd.gdz6rLlb2q/org.macosforge.xquartz:0
_=/usr/bin/env If we let in every environment variable, then all of those would get shipped along with whatever site I build which would a) be wasteful and b) potentially a security leak if you have sensitive info in an env var (I took a few out of the above actually 😄 ). |
I've been thinking trying to get env vars to work with my gatsby site recently too. Might plugging this in be useful? https://www.npmjs.com/package/dotenv-webpack I'm pretty new, not sure how to go about trying to integrate it if it would be helpful. |
Posted this here. This is my current workaround:
Granted, this only let's you have 2 environments. |
I think copying the React App pattern and adding support for |
@0x80 had a good idea that for v1 we could also provide an action creator for directly setting environment variables #1033 (comment) |
If it's helpful in any way, this is what I've done and it seems to work well:
|
@KyleAMathews I’m not sure #1462 fully addressed this since there’s still no way to pass in true ENV vars without adding a file. This precludes e.g. using Netlify’s built-in ENV var management. |
Right #1462 (comment) Would love a PR from you adding support for this! |
Would this project be open to add config files to whitelist non prefixed host env variables? SOME_HOST_ENV_VAR_NAME_1
SOME_HOST_ENV_VAR_NAME_2 // whitelisted env var are either in the whitelist file or prefixed by GATSBY_
var whitelistedVarObject = Object.keys(process.env).reduce(function (acc, key) {
if (key.match(/^GATSBY_/) || whitelist.includes(key)) {
acc[key] = JSON.stringify(process.env[key]);
}
return acc;
}, {}); |
@quentin- did you see this page? We do support .env.production files |
Not sure if what you're proposing is different. If it is, please post a new issue to track the discussion there! |
Hi @KyleAMathews how about if I want to have .env.test or something else .env.beta??? There is any possibility to have more than only develop and production? |
@HuuDuc it's really |
So is there no way of achieving the following while using {
resolve: 'gatsby-source-contentful',
options: {
spaceId: process.env.SPACE_ID,
accessToken: process.env.ACCESS_TOKEN,
},
}, In gatsby-config.js |
The problem here is that so many gatsby plugins use When it comes to deploying to a staging environment this is problematic as you don't want to add the tracking code, but Gatsby forces the |
Big down thumbs for not supporting "REACT_APP_" nor "GATSBY"... :( |
Have there been any updates on this? Trying to get a staging environment going but looks like it's forcing |
I'd like to set some envionment variables and read them from my site, but I haven't found a way to do so.
If I run
FOO=foo npm run dev
There is no way to read that FOO variable, as far as I know.
console.log(process.env)
returns only{NODE_ENV: "development"}
Any way to solve that ?
Is it supported ?
Thanks
The text was updated successfully, but these errors were encountered: