Using Environment Variables in Next.js

Using Environment Variables in Next.js

Keeping your keys safe.


Environment variables are used everywhere nowadays. Libraries such as dotenv help with achieving the same functionalities while bigger frameworks such as Next.js implement their own versions of dotenv.

.env file is a file used on your own system which contains keys for making API calls, database connection strings, and so on. But it is important that you would keep these safe for obvious reasons.

Most of the time, frameworks implement environment variables in roughly the same ways. So reading this article will still be useful for understanding the same principles that you could use in other projects.


Next.js is a simple framework to build web applications using React for templating. It has more features such as routing which I will not get into today.

In your console, run npm initand npm i next react react-dom to install Next.js, React, and ReactDOM via Node Package Manager.

Add the following scripts in your package.json:

"scripts": {  
  "dev": "next dev",
  "build": "next build",
  "start": "next start"

Next, we need to add a /pages/ directory with a index.js in it.

function HomePage() {
    return <div>Welcome to Next.js!</div>

  export default HomePage


Run npm run dev in your terminal, go to localhost:3000 and check if the page is displayed.

nextjs nodejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes. Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer]( "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 Code: https://bit....

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

What is ECMAScript and How is it Different From JavaScript?

Many times developers use JavaScript and ECMAScript synonymously for each other. Though they are very closely linked to each other, it does not mean they are the same is a complete story on the history of JavaScript and how it came into existence. To cut the long story short, ECMA in ECMAScript refers to Europen Computer Manufacturers Association to which JavaScript 1.1 was submitted for standardization back in the year 1997.