Use of environment variables with Netlify and React(Gatsby) - reactjs

I'm using env variables in my react app, so I don't expose my private API keys publicly. They are set in a .env file (which is excluded from git) like so - REACT_APP_ENV_NAME which works fine during development.
I have added those to the Netlify env configuration from within the site settings panel, but they don't seem to work during production (just showing as undefined). Have any of you had the same problem before/managed to fix it?

You need to use GATSBY_ prefix in your environment variables. So, your REACT_APP_ENV_NAME will be GATSBY_REACT_APP_ENV_NAME in your Netlify back office.
You can check for further information in Gatsby documentation, in this Netlify community thread and in Netlify documentation.
From Gatsby documentation:
Please note that you shouldn’t commit .env.* files to your source
control and rather use options given by your Continuous Deployment
(CD) provider. An example is Netlify with its build environment
variables.
GATSBY_API_URL=https://dev.example.com/api
API_KEY=927349872349798
From Netlify documentation:
Gatsby environment variables
Any environment variables prefixed with GATSBY_ will be processed by
Gatsby and made available in the browser for client-side JavaScript
access. Visit the Gatsby docs about environment variables for more
information.

Related

What happens to the .env file when I build my react app with npm run build?

The build version is working perfectly but can't see where it stores the environment variables. Are they hidden or visible to everyone ?
The Create React App (Webpack) build process "folds" the environment variables into constants in the source, and things are bundled as per usual after that.
They're visible to anyone who looks at your app's script files in their browser's View Source or Inspector views.

How to access the environment variables in Amazon S3 for the react application as I have stored the Firebase and Google Api Key in env variables?

REACT_APP_API_KEY=**
REACT_APP_FIREBASE_API_KEY=**
Using in the below style in react js for local environment :
process.env.REACT_APP_FIREBASE_API_KEY
Note : I am using aws code pipeline and code build for ci /cd.
In Code Build you can edit the Environment and add environmental variables. Click Additional configuration in the console to see it. You can export REACT_APP_FIREBASE_API_KEY there and you it should be picked up by npm run build.
You could also use an .env.production file and commit it to source code. These aren't secrets, are added to the deployed code, so there shouldn't be a security issue.

Next JS env variables not working in production

According to Next.js documentation, you need to declare your env variables in next.config.js under env key to read them at build time:
env: {
GOOGLE_ANALYTICS_ID: process.env.GOOGLE_ANALYTICS_ID
},
This is working correctly in my local environment, the variables are hidden from _NEXT_DATA and I can still access them from the client. The problem is when deploying the application for production, it can no longer read from process.env. Has anyone encountered this issue?
Note: I don't want to expose sensitive data. Will adding the NEXT_PUBLIC_ prefix to the env variables expose them to _NEXT_DATA as well?
You should store your env variables in .env (for Next.js versions 9.4 and up) file like it's shown here. In case you are deploying to Vercel refer to this guide

create-react-app build/serve environment variables

Relatively new to working with react. I have an application that is working fine in local docker. I populate a bunch of REACT_APP_ environment variables by exporting them to the environment before starting the docker container.
Now, I'm trying to deploy this to a kubernetes pod by running a yarn build and then serving up the build. I see that the environment variables are available on the pod itself by looking at printenv but the application doesn't appear to be picking them up.
Is there something special with serving a production build of a react-app to get it to see the exported environment variables that I'm missing?
I don't want to embed an .env file into the built docker image for security reasons, so I'm hoping that running a react build via serve can still pick up exported REACT_APP_ environment variables that are set via kubernetes secrets.
So apparently, whenever you build a react application with npm, static files are produced that don't know anything about any environment variables you may try to inject at runtime with Kubernetes.
The article below does a good job of explaining this and why they choose to attach the environment variables to the JavaScript window object since it has application-scope available to it.
Making a React application container environment-aware at Kubernetes deployment

Create-React-App + Heroku: Development, Staging and Production environments

I'm developing an app (front-end app that consumes an API) based on the create-react-app package. I'm using Heroku to deploy and currently have two deployments based on the same codebase, staging and production. These deployments should use different development/staging/production APIs that have different databases.
Is it possible to tell create-react-app to use different env variables based on how I run react-scripts start?
env.development
REACT_API: https://localhost/react_api
env.staging
REACT_API: https://myappstagingapi.heroku.com
env.production
REACT_API: https://myappproductionapi.heroku.com
How would I do this? And is this a good workflow?
Thank you very much!
I had the similar situation having more environments than production and development while deployment was done automatically from Github.
First of all, make sure you are using the deployment buildpack i.e.
https://github.com/mars/create-react-app-buildpack.git
You can add this URL in Settings in your project on Heroku, replacing NodeJS default buildpack for instance.
Full documentation is here:
https://elements.heroku.com/buildpacks/nhutphuongit/create-react-app-buildpack
If you follow the link above, you can see the chapter Environment variables. So, in order that your React App can process custom variables:
Add a one that suits you with REACT_APP_ prefix to your Heroku project environment variables (through Settings in Heroku Dashboard) Note that only the envs with this prefix will be visible in your process.env later so you should amend your code accordingly
Redeploy the application.
This should make the things work. I am sure it is also possible to keep .env file but I prefer to have more control via Heroku Dashboard.

Resources