Set Environment Variable (.env) in NextJS
How to use environment variables (.env) with Next.js

An esteemed software engineer who is passionate about design, problem solving, learning and Project Management. I'm dedicated to crafting seamless interfaces that bridge the gap between design and functionality with a keen eye for creating intuitive and visually appealing user experiences. I thrive on dissecting complex issues and designing innovative solutions that not only meet technical requirements but also exceed user expectations. I strongly believe in the power of collaboration, and my experience working within cross-functional teams has enriched my ability to communicate effectively, contribute ideas, and learn from my peers. I have gained relevant skills in full stack JavaScript web development (MERN STACK), 3D Design and printing, Computer aided manufacturing, Digital marketing, graphic design, user experience and Management through several years of academic study and work experience in both national and international organizations. Currently learning web3 Blockchain development. In addition to my work as the Founder and Design engineer for Shielddeviser, I am also the Program Manager at Vocal Array Academy, Shieldstring Violinist a minstrel with the sole aim of leading people into the presence of God.
My environment variables aren’t working 😱😱😱
You might be in this situation, you want to use a .env file to hide API URL or secret key during development in your NextJS app. Let’s see how we can set things up correctly.
How do I use environment variables in Next.js?
Simple! In the root of your Next.js application create a .env file. Inside this file, declare all environment variables you need like this:
STRIPE_KEY = "*************"
API_KEY = "<Your API KEY>"
Then, in your code you can reference these variables
Using next.config.js
NextJS provides you with the feature of using environment variables in your application. You need to define your variable in a .env.local file in the root folder. Create a.env.local file to declare all your environment variables.
API_KEY = "<Your API KEY>"
The variable defined in the .env.local is now available on the server side. If you console.log the API_KEY in the frontend, you will see undefined in the console of your dev tool.
console.log(process.env.API_KEY)
To get the environment variable available to the frontend we need to configure the next.config.js file.
const nextConfig = {
env:{
API_KEY: process.env.API_KEY
}
}
Now the API_KEY will be available in the frontend and if you console.log, you will get the value.
‼️Note: Restart the server to see the changes
Using ‘NEXTPUBLIC’
The latest version of nextJS comes with the feature to avail environment variables to the frontend without configuring the next.config.js. You have to prefix the variable in .env.local with NEXTPUBLIC
NEXT_PUBLIC_JWT_SECRET = "<Your secret>"
This will load the variable to the frontend with the name NEXT_PUBLIC_JWT_SECRET. You can access the value through process.env.NEXT_PUBLIC_JWT_SECRET
console.log(process.env.NEXT_PUBLIC_JWT_SECRET)
##Using ‘dotenv’ library
If you use the dotenv library to load the environment variable, you can integrate it easily in NextJS. The process follows a similar pattern as configuring the next.config.js. Install the library and create the .env file in the root directory. Initialize the variable with the value as you normally do.
API_KEY = "<Your API KEY>"
Now, we have to configure the next.config.js. First, we need to import the dotenv library.
require("dotenv").config
Now, we have to load the variable to the frontend by adding it to the file.
const nextConfig = {
env:{
API_KEY: process.env.API_KEY
}
}
Now, you will have the environment variables in the frontend.
Conclusion
You can use any method mentioned above to load the environment variable. Thanks for reading the article.




