Why am I getting the "ReplyError: ERR unknown command JSON.SET" with Redis?

Viewed 4610

I was trying to set JSON as value in Redis, and I am getting an error for the following code:

const createClient = require('redis');


async function redisJSONDemo () {
  try {
    const TEST_KEY = 'test_node';

    const client = createClient.createClient();
    await client.connect();

    // RedisJSON uses JSON Path syntax. '.' is the root.
      await client.json.set(TEST_KEY, '.', { node: 'blah' });
    const value = await client.json.get(TEST_KEY, {
      // JSON Path: .node = the element called 'node' at root level.
      path: '.node'
    });

    console.log(`value of node: ${value}`);

    await client.quit();
  } catch (e) {
    console.error(e);
  }
}

redisJSONDemo();

Error:

ReplyError: ERR unknown command JSON.SET, with args beginning with: test_node, ., {"node":"blah"},

How can it be fixed?

1 Answers

There few potential causes:

#1 RedisJSON module is not installed. Try:

redis-cli info modules

Output should contain module:name=ReJSON,ver=... and you should be able to do the following in the redis-cli:

127.0.0.1:6379> json.set test_node $ '{ "node": "blah" }'
OK
127.0.0.1:6379> json.get test_node
"{\"node\":\"blah\"}"

#2 redis npm module is of an older version.

npm -v redis
8.1.4

try npm upgrade redis if yours is older.

The error looks like one, coming from the Redis server, so problem #1 is the most likely cause.

Related