I recently started building a website using Next.js and encountered an issue while trying to integrate Google Tag Manager.
Despite following the tutorial provided in the Next.js Github example, I found myself unable to access my environment variables.
Every time I tried to retrieve the variable, it returned as undefined.
To address this, I created a .env.local file in my project folder, containing a variable for testing purposes:
NEXT_PUBLIC_DB_HOST=localhost
When I attempted to access this variable on my index page with the following code :
console.log("test ", process.env.NEXT_PUBLIC_DB_HOST);
Unfortunately, the console displayed "test undefined".
I even tried moving the variable to an .env file, but that didn't solve the issue either.
Can someone please advise on what mistake I may be making?