Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.env.API_URL is undefined

I built a project using React at the front and Strapi (based on Node.js) at the back.

On the client side, I'm trying to access my api url like so:

const apiUrl = process.env.API_URL || 'http://localhost:1337'
const strapi = new Strapi(apiUrl);

But process.env.API_URL is undefined. If I log process.env all I get is an object containing:

NODE_ENV: "development"
PUBLIC_URL: ""

How can I access my api_url ? I'm guessing there is a file where I should define API_URL myself?

like image 295
sir-haver Avatar asked Nov 28 '22 21:11

sir-haver


1 Answers

Assuming you are using create react app, Make sure you add the prefix REACT_APP like REACT_APP_API_URL. In your project src create a file .env.development or .env.production and add the your value with a key with the prefix

like image 108
TRomesh Avatar answered Dec 05 '22 05:12

TRomesh