I have deployed a next js application to netlify using git and I have a .env.local
file that stores the backend route url that I use everywhere throughout the app when making fetch requests. The problem is that after deployment the process.env.NEXT_PUBLIC_BACKEND_ROUTE returns undefined.
The .env.local
file:
NEXT_PUBLIC_BACKEND_ROUTE=https://[the name of the url].herokuapp.com/
An example of a page using the environment varible:
JavaScript
x
27
27
1
import axios from 'axios';
2
import { useRouter } from 'next/router';
3
import { useEffect } from 'react';
4
5
export default function Home() {
6
const router = useRouter();
7
useEffect(() => {
8
axios
9
.get(`${process.env.NEXT_PUBLIC_BACKEND_ROUTE}/findAllPictures`)
10
.then((doc) => {
11
const arr = doc.data;
12
13
if (arr.length !== 0) {
14
const mappedArr = arr.map((obj) => {
15
return obj.id;
16
});
17
const amount = mappedArr.length;
18
const rand = Math.floor(Math.random() * amount);
19
const routeId = mappedArr[rand];
20
router.push(`/view/${routeId}`);
21
}
22
});
23
}, [null]);
24
return <div></div>;
25
}
26
27
Advertisement
Answer
There are two possibilities I see
your
.env.local
file is not in rootSome weird formatting issue is going on with the variables. In that case, try surrounding your variable in quotes:
NEXT_PUBLIC_BACKEND_ROUTE=”https://[the name of the url].herokuapp.com/”