Next.js Environment Variables
Last Updated :
05 Jan, 2023
In this article, we are going to see how to use environment variables in Next.js. Environment variables in Next.js are a way to set configuration values that are used by your application. They can be used to store data such as the name of your company, the port your application will run on, or any other configuration values you need.
You can set environment variables in Next.js using the .env file. You have to create this file at the root of your project. You can use this file to set environment variables for your application.
Follow the below steps to add environment variables in the Next.js application:
Creating Next.js Application:
Step 1: To create a new NextJs App run the below command in your terminal:
npx create-next-app GFG
Step 2: After creating your project folder (i.e. GFG ), move to it by using the following command:
cd GFG
Project Structure: It will look like this.

Example: Add Environment Variables, for example, let's create a new environment variable and display the value of that variable on the homepage. Create a new .env file and add the below content.
NEXT_PUBLIC_DEMO_VARIABLE=random_value
Now in your index.js file add the below content:
JavaScript
// Importing the Link component
import Link from 'next/link'
export default function Homepage() {
return (
<div>
This is the Homepage page - GeeksforGeeks
<h2>
Environment Variable :-
{process.env.NEXT_PUBLIC_DEMO_VARIABLE}
</h2>
</div>
)
}
Step to run the application: Now run the application with the below command:
npm run dev
Output: Here we are accessing the value of the environment variable using 'process.env'.

Similar Reads
Environment Variables in Java In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
5 min read
Reading Environment Variables From Node.js Environment Variable: The two fundamental concepts of any programming language are variables and constants. As we know that constants and variables both represent the unique memory locations that contain data the program uses in its calculations. The variable which exists outside your code is a part
2 min read
Node.js __dirname Variable The __dirname string gives the directory path of the current module, this is also similar to that of path.dirname() of the filename. Return Value : It returns the directory path of the current module. Example 1: Let's create a file main.js main.js import path from 'path'; const __dirname = path.reso
1 min read
How To Set Environment Variables In Jenkins ? Jenkins is a widely used open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) processes. One powerful feature of Jenkins is its capability to configure environment variables, which are important for various functions like defining parameters, paths, a
6 min read
Environment Variables are Undefined in Next.js App Environment variables play a crucial role in web development, especially in configuring sensitive information such as API keys, database URLs, and other configuration settings. If your environment variables are showing up as undefined in your Next.js app, it can disrupt functionality. This issue typ
3 min read