content/develop/clients/nodejs/_index.md
node-redis is the Redis client for Node.js/JavaScript.
The sections below explain how to install node-redis and connect your application
to a Redis database.
{{< note >}}node-redis is the recommended client library for Node.js/JavaScript,
but we also support and document our older JavaScript client
[ioredis]({{< relref "/develop/clients/ioredis" >}}). See
[Migrate from ioredis]({{< relref "/develop/clients/nodejs/migration" >}})
if you are interested in converting an existing ioredis project to node-redis.
{{< /note >}}
node-redis requires a running Redis server. See [here]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis Open Source installation instructions.
You can also access Redis with an object-mapping client interface. See [RedisOM for Node.js]({{< relref "/integrate/redisom-for-node-js" >}}) for more information.
To install node-redis, run:
npm install redis
Connect to localhost on port 6379.
{{< clients-example set="landing" step="connect" lang_filter="Node.js" description="Foundational: Create a client connection to a Redis server using node-redis" difficulty="beginner" >}} {{< /clients-example >}}
Store and retrieve a simple string.
{{< clients-example set="landing" step="set_get_string" lang_filter="Node.js" description="Foundational: Set and retrieve string values using SET and GET commands" difficulty="beginner" >}} {{< /clients-example >}}
Store and retrieve a map.
{{< clients-example set="landing" step="set_get_hash" lang_filter="Node.js" description="Foundational: Store and retrieve hash data structures using HSET and HGET commands" difficulty="beginner" >}} {{< /clients-example >}}
To connect to a different host or port, use a connection string in the format redis[s]://[[username][:password]@][host][:port][/db-number]:
createClient({
url: 'redis://alice:[email protected]:6380'
});
To check if the client is connected and ready to send commands, use client.isReady, which returns a Boolean. client.isOpen is also available. This returns true when the client's underlying socket is open, and false when it isn't (for example, when the client is still connecting or reconnecting after a network error).
When you have finished using a connection, close it with client.quit().
{{< clients-example set="landing" step="close" lang_filter="Node.js" description="Foundational: Close a Redis client connection using the quit() method" difficulty="beginner" >}} {{< /clients-example >}}
The node-redis website has more examples.
The Github repository also has useful
information, including a guide to the
connection configuration options you can use.
See also the other pages in this section for more information and examples: