next.js environment variables are undefined (Next.js 10.0.5)
Full error message
I am coding a website with Next.js and I tried to add google Tag Manager.
I followed the tutorial on the Next.js Github example but for some reasons I can't access to my environment variables.
It says my variable is undefined.
I created a file .env.local on my project folder (at the same level as components, node_modules, pages, etc)
In this file I created a variable like this (test purpose) :
NEXT_PUBLIC_DB_HOST=localhost
And on my index page I tried this code :
console.log("test ", process.env.NEXT_PUBLIC_DB_HOST);
But in my console I get a "test undefined".
I tried to put my variable into an .env file instead, without success.
What I am doing wrong ?Solutionsource: stackoverflow \u2197
This envs just works in Server Side. To access this envs in Client Side, you need declare in the next.config.js This way: module.exports = { reactStrictMode: true, env: { BASE_URL: process.env.BASE_URL, } }
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/c603997e0acd08e179fb7f980a4a69508b302934054ffb399ecba6d19b16b5cfhash \u00b7 c603997e0acd08e179fb7f980a4a69508b302934054ffb399ecba6d19b16b5cf