Next.js Discord

Discord Forum

Browser environment vars not working

Answered
Parson Russell Terrier posted this in #help-forum
Open in Discord
Parson Russell TerrierOP
Hello,

I have a next js project setup with a .env.local file located in my src/ folder.

The .env.local file looks like:

NEXT_PUBLIC_API_BASE_URL="http://127.0.0.1:8787"


However when I attempt to print out the env var from a component it is undefined. Am I missing something here?
Answered by European sprat
root of your repo, not in src
View full answer

9 Replies

European sprat
root of your repo, not in src
Answer
Parson Russell TerrierOP
Ah thank you!
European sprat
did it work?
Parson Russell TerrierOP
Yes
One more question, is there any way to disable next from appending localhost:3000 in fetch calls?

 const response = await fetch(`{${process.env.NEXT_PUBLIC_API_BASE_URL}/`, { cache: "no-cache" });
  console.log(`{${process.env.NEXT_PUBLIC_API_BASE_URL}/`);
That tries to fetch http://localhost:3000/%7Bhttp:/127.0.0.1:8787%7D
European sprat
something about that doesn't look right like you have an extra {
Parson Russell TerrierOP
Oh yeah there was thanks
fixed