I have added a.env file to my root folder and added the below variable.
REACT_APP_API_URL=http://localhost:4000
I have used this in a component as below.
process.env.REACT_APP_API_URL
After adding the above, I have restarted the reactJS application and run the application via npm start. I see the below message in the console, but still, the variable gives an undefined value.
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - Loaded env from D:\ReactJsProj.env
info - Using webpack 5. Reason: no next.config.js https://nextjs.org/docs/messages/webpack5
it seems you just want to change your port, not the whole app url. so just do this in your .env
file:
PORT=4000
and then restart the dev server. it will do the job!
In nextjs you should add env variables in .env.locale
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.