What is the difference between .env.local and .env.development.local? - reactjs

I used a create-react-app.
And created environment variable files. .env.local, .env.development.local
I found that .env.development ispreferred over .env.local
And env.development.local seems to have prioty over .env.development.
If so, .env.development.local is used to override .env.development what is the purpose of .env.local?

Here's the priority of the files for the development build and the production build:
Dev.: (npm start): .env.development.local, .env.local, .env.development, .env
Prod.: (npm run build): .env.production.local, .env.local, .env.production, .env
If you ever want to use something in your local environment without being specific to the development build or the production build, you can add some variables to your .env.local file.
Source : https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used

In .env.local you can set up environment variables that are specific to your local machine and it doesn't have to be on development mode to work, so variables there will work for both development and production.

Related

.env.local file in Next.js not setting up properly

when I create a .env.local file in my project root, it's not setting up properly.
in the file, I have my API key like so
API_KEY=SOME_API_KEY
Then when i try to access it in getServerSideProps with process.env.API_KEY, it doesn't work. When I console.log(process.env.API_KEY) i get undefiend. Which I assume is because the file isn't set up properly?
I even tried installing dotenv but I know you don't need that with Next.js.
next config file
module.exports = {
reactStrictMode: true,
}
You’ll notice that the browser will log undefined. This happens since the environment variable is only defined on our server so far.
Since Next.js 9.4, Next.js will make all environment variables that start with NEXT_PUBLIC_ available to the client without any further configuration!
Create .env (all environments), .env.development (development environment), .env.production (production environment), .env.local (local enviroment)
Add the prefix NEXT_PUBLIC_ to all of your environment variables.
NEXT_PUBLIC_BASE_URL=http://localhost:3000/
Use with prefix process.env
process.env.NEXT_PUBLIC_BASE_URL
Stop the server and restart it:
npm run dev
OR
yarn dev
.env.local works with +v9.4.
if you are using older version please try with next config file https://nextjs.org/docs/api-reference/next.config.js/environment-variables.
Note:
Next.js will replace process.env.customKey with 'customKey' at build time. Trying to destructure process.env variables won't work due to the nature of webpack
Restarting the server solved it for me as in #Igmer Rodriguez comment above .

Build react app multiple times with different environment variables

I have a development environment file .env.development and a production .env file for my React web app.
The .env.development has a value REACT_APP_NAME=My Test App and the .env file has the value REACT_APP_NAME=My Blue App.
This works if there is only one customer (called Blue) that uses my app. The .env has the customer address and more customer specific values as well.
Now I want to deliver the app to multiple customers with multiple customer specifications in multiple env files.
My idea is to store customer specific env files:
.env.blue
.env.red
.env.yellow
When I now (somehow) build the app, I'll get three folders
build_blue
build_red
build yellow
and each folders consumed the specific environment for the build.
Is this somehow possible?
My current workaround would be:
Rename .env.blue to .env
yarn build
publish to ftp server
Rename .env to .env.blue
Rename .env.red to .env
yarn build
...
I build a little shell script that does the job.
I'd like to share it, as someone else forces the same solution:
mv .env env
for CURR in .env.red .env.blue .env.yellow
do
mv $CURR .env
yarn build
mv build build_$CURR
cp .env build_$CURR/.env
mv .env $CURR
done
mv env .env
After that you can load the different build folders to the different servers and move the .env file to the root.

Multiple env files choosing which one on docker - create react app

I am building a react app using cra, so the problem is the application just has the client side code which means there is no nodejs part.
I have two different environments one is development and one is production, as cra tells there is a order of preference:
.env
.env.development
.env.production
So if .env.production file is there in repo it will take that one and use that config based on the script that I give, if I use npm run build it will use .env.production and if I use npm start it will use .env.development if the file is there.
So I can add .env, .env.development, .env.production, but when I build the image in the Docker I can give only one command either it should be npm start or npm run build. So how should I solve this?
Install a local development environment; typically your only host dependency will be Node itself. Use the .env.development file there, via something like the Webpack dev server, with a command like yarn start.
Use Docker principally as a deployment mechanism. Your Dockerfile can build your application using the .env.production file, and then copy it into something like an Nginx container that doesn't need Node at all. It should follow the pattern in the CRA Creating a Production Build docs. Loosely,
FROM node:lts AS build
WORKDIR /app
COPY package.json package.lock .
RUN npm install
COPY . .
ENV NODE_ENV=production
RUN npm run build
FROM nginx
COPY --from=build /app/build /usr/share/nginx/html
# Base image provides default EXPOSE, CMD
This pattern gets around all of the difficulties of trying to make Docker act like a local development environment (filesystem permissions, node_modules not updating, live reloading being flaky, ...) by just using an actual local development environment; but at deployment time you get the benefits of a self-contained Docker image with no host dependencies.

dotenv : how to set custom path

This is my architecture and I want to access the.env file
I tried all the solutions, __dirname, find-config, ckey and read all the stack solutions. I can't understand why my .env file is not loaded....
console output is always :
{NODE_ENV: "development", PUBLIC_URL: ""}
If you're using create-react-app to bootstrap your application, the react-scripts module handles setting up environment variables for you. However, there's a catch. All React environment variable needs to be prefixed with REACT_APP. Thus, your environment variable would be: REACT_APP_MY_ENV_VARIABLE.
You should not import dotenv. After changing .env files, you must restart the development server. This is the excerpt from the create-react-app docs. The .env must appear in the root of your project.
Note: You must create custom environment variables beginning with
REACT_APP_. Any other variables except NODE_ENV will be ignored to
avoid accidentally exposing a private key on the machine that could
have the same name. Changing any environment variables will require
you to restart the development server if it is running.
You can read more about environment variables and .env files with create-react-app in the create-react-app documentation.
If you really need to set a custom path, you can use env-cmd. It requires small changes in scripts section like this:
// from
"start": "react-scripts start"
// to
"start": "env-cmd -f ./custom/path/.env react-scripts start"

Gatsby - Unable to set environment variables

In my gatsby-config.js I've used the dotenv package to set env vars
require("dotenv").config({
path: `.env.${process.env.DEPLOY_ENV}`,
});
and then in my package.json a script to deploy to different environments
"deploy:staging": "DEPLOY_ENV=staging gatsby build --prefix-paths && s3-deploy ..."
In my src/html.js, I have an asset that I want to include
<script src={`//${process.env.ASSET_HOST}/app.js`}></script>
When I log the DEPLOY_ENV from gatsby-config.js it is set to staging, however, when I log process.env in src/html.js, ASSET_HOST is set as the one in my .env.production file, so when I deploy to staging it uses assets from my production host.
I think DEPLOY_ENV (as NODE_ENV) is a reserved environment variable. That's why DEPLOY_ENV=staging is not working in your case.
The Gatsby docs on environment variables advices to use a secondary environment variable for additionnal environement support.
You can add a .env.staging file in your root folder, where you put your ASSET_HOST env var.
Then run gatsby with ACTIVE_ENV=staging gatsby develop

Resources