node-redis throws The client is closed when you send a command
ClientClosedError: The client is closedShort answer
node-redis throws ClientClosedError when the client's socket is not open. That happens when connect() was never awaited, when close() or destroy() already ran, or when reconnecting stopped. Find which one with client.isOpen and an error listener, then create and connect the client once.
Fix it fast
1Find out which cause you have
Log the client's state and every event right where the error appears. Do this before changing any code.
client.on('error', (err) => console.error('redis error', err)); client.on('end', () => console.log('redis connection ended')); client.on('terminated', (cause) => console.error('redis gave up', cause)); console.log('isOpen', client.isOpen, 'isReady', client.isReady);2Match what you see, then jump to its fix
- 1
isOpen is false on the very first command, and no end event was loggedThe client was never connected before a command ranFix - 2
An end event is logged, then later requests failclose() or destroy() ran, and the client is used againFix - 3
A terminated event is logged before the errorReconnecting was given up by the reconnect strategyFix
- 1
What does the error look like?
ClientClosedError: The client is closed
Cause 1 of 3
The client was never connected before a command ran
1Check
createClient() only builds the object. Commands sent before the socket is open are rejected with this error. Look for a missing await or a command run at import time.
grep -rn "createClient\|\.connect()" --include=*.js --include=*.ts --exclude-dir=node_modules .2Fix
Create the client in one module, register the error listener, and await connect() there. Import that module wherever Redis is used.
// redis.js
import { createClient } from 'redis';
export const client = await createClient()
.on('error', (err) => console.error('Redis Client Error', err))
.connect();3Verify
A command run right after startup succeeds.
node -e "import('./redis.js').then(async ({client}) => { console.log(await client.ping()); await client.close(); })"Confirmed by github.com
Cause 2 of 3
close() or destroy() ran, and the client is used again
1Check
Search for close(), quit() or destroy() calls in request handlers, tests or shutdown hooks that also run while the app is still serving. Calling quit() or close() on a client that is already closed throws the same error.
grep -rn "\.close()\|\.quit()\|\.destroy()" --include=*.js --include=*.ts --exclude-dir=node_modules .2Fix
Share one long-lived client for the whole process and close it only once, on shutdown. If you must replace a client, create and connect a new one instead of reusing the old object.
process.on('SIGTERM', async () => {
if (client.isOpen) await client.close();
process.exit(0);
});3Verify
Requests keep working across many calls, and only SIGTERM triggers the end event.
for i in 1 2 3 4 5; do curl -s -o /dev/null -w '%{http_code} ' http://localhost:3000/health; doneConfirmed by github.com
Cause 3 of 3
Reconnecting was given up by the reconnect strategy
1Check
When reconnectStrategy returns false or an Error, the client stops reconnecting and emits terminated, then the companion error. Look at the strategy in your client options.
grep -rn "reconnectStrategy" --include=*.js --include=*.ts --exclude-dir=node_modules .2Fix
Return a delay so the client keeps retrying, or destroy the dead client and build a new one when it terminates. The default strategy is exponential backoff capped at 2000 ms plus jitter.
const client = createClient({
socket: { reconnectStrategy: (retries) => Math.min(2 ** retries * 50, 2000) },
});
client.on('terminated', (cause) => {
console.error('client will not reconnect:', cause);
queueMicrotask(() => client.destroy());
});3Verify
Stop Redis, start it again, and watch commands resume without a restart of the app.
sudo systemctl stop redis-server && sleep 3 && sudo systemctl start redis-serverConfirmed by github.com
How do I stop it coming back?
- Keep one client per process in one module, connected with top-level await, and import it everywhere.
- Always attach an error listener: without one, an error is thrown and can exit the process.
- Close the client only in the shutdown handler, never inside a request.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Node.js cannot reach Redis: ECONNREFUSED on port 6379
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
ECONNREFUSED on 6379 means no Redis server accepted the connection at the address the client used.
4 causes, each with a fixDatabases
node-redis throws ConnectionTimeoutError on connect
Redis Client Error ConnectionTimeoutError
node-redis raises ConnectionTimeoutError when the socket does not connect within socket.connectTimeout, which is 5000 ms by default.
3 causes, each with a fixDatabases
Node.js in Docker Compose cannot find mysql2/promise
The app works on the host but fails after docker compose up
mysql2/promise is a path inside the mysql2 package.
2 causes, each with a fixDatabases
Elasticsearch rejects requests with circuit_breaking_exception: Data too large
circuit_breaking_exception with a reason that contains Data too large
A circuit_breaking_exception with 'Data too large' means Elasticsearch refused work to protect a node's JVM heap.
3 causes, each with a fixDatabases
Jedis cannot borrow a connection from its pool
Caused by: java.net.SocketTimeoutException
The pool could not give the code a working connection in time.
3 causes, each with a fixDatabases
JupyterLab cannot determine its build status without Node.js
Could not determine jupyterlab build status without nodejs
JupyterLab itself runs without Node.js, but source extensions need Node.js to rebuild it.
3 causes, each with a fix