I am using create react app to bootstrap my app.
I have added two .env
files .env.development
and .env.production
in the root.
My .env.development
includes:
API_URL=http://localhost:3000/api
CALLBACK_URL=http://localhost:3005/callback
When I run my app using react-scripts start
and console out process.env
it spits out
{ NODE_ENV: "development", PUBLIC_URL: "" }
I've tried different things, but its just not picking up the veriables in my development file, what am I doing wrong?!
Directry structure is:
/.env.development
/src/index.js
Package.json script is:
"start": "export PORT=3005; npm-run-all --parallel server:start client:start",
"client:start": "export PORT=3005; react-scripts start",
"server:start": "node server.js",
"build": "react-scripts build",
Edit:
@jamcreencia correctly pointed out my variables should be prefixed with REACT_APP
.
Edit 2
It works okay if I name the file .env
but not if I use .env.development
or .end.production
package.json
file ? – Steve Chamaillardprocess.env
is something that the back-end (Node or whatever you're using) can read. The front-end bundle has no idea whatprocess.env
is as it runs in the browser. You can configure webpack to pass it in the bundle when bundling, or even easier you can pass it from the back-end in the index file you're rendering as a global variable. – Raul Renereact-scripts start API_URL=http://localhost:3000/api CALLBACK_URL=http://localhost:3005/callback
if you still don't see them i'd restart my system to lower memory usage and try again usually this resolves it for me. – Femi Oni