1
Hello!
I’m starting now with Reacjs.
Usually in Nodejs I create a folder environments
and put the index.ts
or index.example.ts
, with values there
DB=nome_banco
HOST=localhost
USER=username
When I started using Reactjs I tried to use the same logic, creating the folder and the files to use the api
dev
HOST=http://localhost:3000
prod
HOST=http::/dominio.com.br:1234
Every time, before I give the build I have to go on Environments to change the host.
I tried to use that tutorial, even worked for localhost, I created two env.Development and env.Production files with the following sample content:
Development
REACT_APP_HOST=http://localhost:3001
Production
REACT_APP_HOST=http://dominio.com.br:1234
And I put in package.json the following:
"scripts": {
"test": "react-scripts test",
"eject": "react-scripts eject",
"start": "env-cmd -f .env.development react-scripts start",
"build": "env-cmd -f .env.production react-scripts build"
},
But when you build it, it only takes the localhost data
Where can I be missing?
Whenever I want a parameter I have to use
REAT_APP_
in the prefix?– adventistaam
Yes. You should always prefix the variables that will be used in the CRA with
REACT_APP_
. Any other variables, except those prefixed byNODE_ENV
(readonly) will be ignored. The reason is that some projects use the same.env
pro React and pro backend. Hence, a database password that is on.env
could be exposed in the production Bundle, since the variables are "hardcodada" in the final compilation. That is, never write aREAT_APP_SEGREDO_ULTRA_SECRETO
.– Rodrigo Amaral
Example: https://i.imgur.com/jnnger0.png
– Rodrigo Amaral
Thanks, it worked out
– adventistaam