It is like the environment variables are not being properly loaded. Here are a few steps you can take to troubleshoot the issue:
Check if you have properly installed the required packages to handle environment variables. Usually, dotenv package is used to load environment variables from a .env file. Make sure you have it installed by running:
npm install dotenv
Ensure that the .env.local file is in the correct location, which is the root directory of your project.
Make sure you are calling the config() method from the dotenv package to load the environment variables before trying to access them. Place this code at the beginning of your application entry point (e.g., index.js, app.js, or server.js):
require('dotenv').config();
Check the spelling of the environment variables. Make sure they match exactly with the variable names in the .env.local file.
Restart your application after making changes to the environment variables or their loading process.
If you’ve followed these steps and still encounter issues, please double-check the .env.local file and verify that the environment variables are set correctly. If the problem persist reply to this answer.
3
Answers
Update: I moved my .env directory from utils folder to root directory and it worked for me.
It is like the environment variables are not being properly loaded. Here are a few steps you can take to troubleshoot the issue:
Check if you have properly installed the required packages to handle environment variables. Usually,
dotenv
package is used to load environment variables from a.env
file. Make sure you have it installed by running:Ensure that the
.env.local
file is in the correct location, which is the root directory of your project.Make sure you are calling the
config()
method from thedotenv
package to load the environment variables before trying to access them. Place this code at the beginning of your application entry point (e.g.,index.js
,app.js
, orserver.js
):Check the spelling of the environment variables. Make sure they match exactly with the variable names in the
.env.local
file.Restart your application after making changes to the environment variables or their loading process.
If you’ve followed these steps and still encounter issues, please double-check the
.env.local
file and verify that the environment variables are set correctly. If the problem persist reply to this answer.# To expose a public environment variable in Next.js, you have to prefix the environment variable with NEXT_PUBLIC
Example:
You can use it like this: