dotenv : how to set custom path - reactjs

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"

Related

Using environment variable in create-react-app package.json proxy

I would like to set the "proxy"-property in the package.json file of a create-react-app project to the value of an environment variable called "REACT_APP_API_BASE_URL". The variable is stored in the .env.development.local file. I can't find the right way to do this. What is the right notation to access the variable? Each way I tried ended in the following error, when starting the app with npm start:
When "proxy" is specified in package.json it must start with either http:// or https://
This error probably occurred because the value isn't accessed correct.
If you specify in your .env.development.local:
REACT_APP_API_BASE_URL=[your url]
you can use source .env.development.local to store the value and then use it using $REACT_APP_API_BASE_URL.
For example in your package.json you could do this:
"scripts": {
"deploy": "react-scripts build; source .env; ./scripts/deploy.sh $REACT_APP_API_BASE_URL"
}

Create React App: Set HTTPS flag in package.json based on .env variable

To enable create react app to run in SSL mode, we need to set HTTPS=true in the package.json like this
"start": "HTTPS=true react-scripts start",
However, I would like this only in production and not when development happens locally. I have a .env file, and would like to use a flag REACT_APP_USE_SSL=true in production and then REACT_APP_USE_SSL=false locally.
My question is, how can I use this environment variable in package.json? Or is there another way to seamlessly switch between HTTP and HTTPS mode for development and production environment?
As mentioned in the comment by #kiranvj, (and also after the reading the doc myself), this is easier than the other complicated solutions I was trying to implement.
You can just set HTTPS=true or HTTPS=false in your .env file and create react app will pick it up.

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

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.

ReactJs - Webpack and environment variable

Im tring to build my ReactApp using an environment variable system.
Following online guides, I've create two different files
.env.developtment and .env.production
using the syntax for variables:
REACT_APP_BASE_SERVER_URL=myapp/
During development (using npm start to start the server), development file is loaded perfectly and all variable are stored in process.env global.
Unfortunally after compiling it with webpack, process.env is empty.
Im compiling my code with:
./node_modules/.bin/webpack --config webpack.config.js --env.production --progress --env.NODE_ENV=production --colors
and in my webpack there is no process.env override (using DefinePlugin is the common mistake).
Following a similar question on this site, I've tried to put this command in the config to avoid any process override:
node: {process: false}
but with this process.env will be totally undefined instead of empty ( {} ).
Is it possible to use this kind of environment system or .env files are only supported with npm build?
Sending the env as an argument does not set the application environment. Said so, I considering you are doing it inside your config. What would set up few env values is the option mode as development or production, which is available on webpack4 (You do -p or -d for the previous versions).
I dont know why you said using webpack.DefinePlugin is a common mistake. I have been using it and it works very well.
In last case, if you are sure you process.env should not be empty I would suggest you to do the syntax process.env["expected_attr"].

React env variables with .env

I'm trying to follow docs on adding env variables from react-create-app without success:
https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-custom-environment-variables
inside root of the document I have a ".env" file (default .env properties)
.env file contains only one variable 'REACT_APP_API_HOST=http://localhost:8080'
trying to access process.env inside my app (created with create react app) gives me undefined
This is app.js where I'm trying to access process.env without success.
I can't access process.env inside the code. Is there any working example on how to do it?
You can use .env file on root.
start: react-scripts start - uses .env.development
build_staging: "set REACT_APP_ENV=staging & react-scripts build" - uses .env.staging
build: "react-scripts build" - uses .env.production
In your package.json you will eventually have to add NODE_ENV=development at your start script. E.g. NODE_ENV=development && node scripts/start.js for the ejected create-react-app and NODE_ENV=development react-scripts start for the unejected one.
Edit: Apparently NODE_ENV=development is not required since it is already hardcoded when you run the start or build script. Per the docs your custom environment variables should have the following format REACT_APP* as you have already done.
A snippet would be helpful.

Resources