---
title: "How to set up and use environment variables in a Node.js application?"  
description: "How to set up and use environment variables in a Node.js application?"  
author: "Revati S Misra"  
published: 2023-09-28  
updated: 2023-10-04  
canonical: https://www.mindstick.com/forum/159994/how-to-set-up-and-use-environment-variables-in-a-node-js-application  
category: "node.js"  
tags: ["javascript", "node js", "environment variable"]  
reading_time: 3 minutes  

---

# How to set up and use environment variables in a Node.js application?

How to set up and use [environment](https://yourviews.mindstick.com/view/308/need-to-create-a-supportive-work-environment-for-women) [variables](https://www.mindstick.com/articles/715/php-variables) in a [Node.js](https://www.mindstick.com/articles/1499/upload-and-download-file-in-node-js) [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)?

## Replies

### Reply by Aryan Kumar

Setting up and using environment variables in a Node.js application is essential for configuring your application's behavior without hardcoding sensitive information or configuration details into your code. Here's a step-by-step guide on how to set up and use environment variables in a Node.js application:

## 1. Create a .env File (Optional):

- You can create a **.env** file in the root directory of your Node.js project to store environment variables.
- Example **.env** file:

```plaintext
PORT=3000
DATABASE_URL=mongodb://localhost/mydb
SECRET_KEY=mysecretkey
```

## 2. Install the dotenv Package (Optional):

- If you choose to use a **.env** file, you can install the **dotenv** package to load environment variables from the file.
- Run the following command to install **dotenv**:

```plaintext
npm install dotenv
```

## 3. Import and Configure dotenv (Optional):

- In your Node.js application entry point (e.g., **app.js** or **index.js**), import and configure **dotenv** to load environment variables from the **.env** file (if used).

```plaintext
require('dotenv').config();
```

This line should be placed at the top of your entry point file.

## 4. Access Environment Variables:

- You can access environment variables anywhere in your Node.js application using **process.env**.
- For example, to access the **PORT** environment variable:

```plaintext
const port = process.env.PORT || 3000;
```

This code checks if the **PORT** environment variable is set; if not, it defaults to **3000**.

## 5. Set Environment Variables (Deployment):

- When deploying your Node.js application to a production environment, you should set environment variables specific to that environment.
- For example, if you are deploying on a hosting platform or server, you can set environment variables through the hosting provider's dashboard or command-line tools.

## 6. Handling Sensitive Information:

- Environment variables are a secure way to store sensitive information such as database credentials or API keys.
- Store sensitive information in environment variables and never commit them to version control repositories.

## 7. Using Environment Variables in Configuration:

- You can use environment variables to configure various aspects of your application, such as database connections, API keys, and application settings.
- Here's an example of using environment variables to configure a MongoDB connection

```plaintext
const mongoose = require('mongoose');

const databaseURL = process.env.DATABASE_URL;

mongoose.connect(databaseURL, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('Connected to MongoDB');
  })
  .catch((error) => {
    console.error('Error connecting to MongoDB:', error);
  });
```

## 8. Testing Locally:

- While developing locally, you can create a **.env** file (if needed) and set environment variables for testing purposes.
- Ensure that you have the necessary environment variables defined for your local development environment.

By following these steps, you can effectively set up and use environment variables in your Node.js application, enhancing security and flexibility in managing configuration and sensitive information.


---

Original Source: https://www.mindstick.com/forum/159994/how-to-set-up-and-use-environment-variables-in-a-node-js-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
