# Connecting with Node.js

This guide explains how to establish a connection between a Node.js application and a Redis database using the [<span class="s2">redis</span>](https://www.npmjs.com/package/redis) package. It walks through the necessary setup, configuration, and execution of a simple Redis command.

## **Variables**

To successfully connect to a Redis instance, you’ll need to provide the following parameters. These can typically be found on the Elestio service overview page.

<table border="1" id="bkmrk-variable-description" style="width: 100%; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="width: 12.2747%; border-color: rgb(0, 0, 0);">**Variable**

</th><th style="width: 34.5668%; border-color: rgb(0, 0, 0);">**Description**

</th><th style="width: 53.1585%; border-color: rgb(0, 0, 0);">**Purpose**

</th></tr></thead><tbody><tr><td style="width: 12.2747%; border-color: rgb(0, 0, 0);">`HOST`

</td><td style="width: 34.5668%; border-color: rgb(0, 0, 0);">Redis hostname (from Elestio service overview)

</td><td style="width: 53.1585%; border-color: rgb(0, 0, 0);">The address of the server hosting your Redis instance.

</td></tr><tr><td style="width: 12.2747%; border-color: rgb(0, 0, 0);">`PORT`

</td><td style="width: 34.5668%; border-color: rgb(0, 0, 0);">Redis port (from Elestio service overview)

</td><td style="width: 53.1585%; border-color: rgb(0, 0, 0);">The port used for the Redis connection. The default Redis port is 6379.

</td></tr><tr><td style="width: 12.2747%; border-color: rgb(0, 0, 0);">`PASSWORD`

</td><td style="width: 34.5668%; border-color: rgb(0, 0, 0);">Redis password (from Elestio service overview)

</td><td style="width: 53.1585%; border-color: rgb(0, 0, 0);">Authentication key used to connect securely to the Redis instance.

</td></tr></tbody></table>

These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.

[![Screenshot 2025-05-19 at 12.23.05 PM.jpg](https://docs.elest.io/uploads/images/gallery/2025-05/scaled-1680-/screenshot-2025-05-19-at-12-23-05-pm.jpg)](https://docs.elest.io/uploads/images/gallery/2025-05/screenshot-2025-05-19-at-12-23-05-pm.jpg)

## **Prerequisites**

**Install Node.js and NPM**

- Check if Node.js is installed by running:

```
node -v
```

- If not installed, download and install it from [nodejs.org](https://nodejs.org).
- Confirm <span class="s1">npm</span> is installed by running:

```
npm -v
```

**Install the redis Package**

The <span class="s2">redis</span> package enables communication between Node.js applications and Redis.

```
npm install redis --save
```

## **Code**

Create a new file named `<span class="s2">redis.js</span>` and add the following code:

```javascript
const redis = require("redis");

// Redis connection configuration
const config = {
  socket: {
    host: "HOST",
    port: PORT,
  },
  password: "PASSWORD",
};

// Create a Redis client
const client = redis.createClient(config);

// Handle connection errors
client.on("error", (err) => {
  console.error("Redis connection error:", err);
});

// Connect and run a test command
(async () => {
  try {
    await client.connect();
    console.log("Connected to Redis");

    // Set and retrieve a test key
    await client.set("testKey", "Hello Redis");
    const value = await client.get("testKey");
    console.log("Retrieved value:", value);

    // Disconnect from Redis
    await client.disconnect();
  } catch (err) {
    console.error("Redis operation failed:", err);
  }
})();
```

To execute the script, open the terminal or command prompt and navigate to the directory where `<span class="s1">redis.js</span>` is located. Once in the correct directory, run the script with the command:

```
node redis.js
```

If the connection is successful, the output should resemble:

```
Connected to Redis  
Retrieved value: Hello Redis
```