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

2

12

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?

Larsen answered 5/1, 2022 at 7:31 Comment(1)
S
20

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.

Smoulder answered 5/1, 2022 at 14:19 Comment(4)
I did run the command redis-cli info modules. and I do not get any output. And yes I am not able to "json set" in the redis-cli. Can you help me further or could provide any resources? Thanks any way.Larsen
I'm glad that helped! RedisJSON is a module that needs to be installed/activated on Redis. Take a look at oss.redis.com/redisjson - the easiest way to try it would be either Redis Cloud or the docker image.Smoulder
it should be -p 6369:6379 in the docker command. 6379 (second number) is the standard port inside the docker container and 6369 is how you will see it on the host machine.Smoulder
hey it works. I checked it with redis-commander too. Thanks for the help @Anton.Larsen
P
2

You could also try with the redis-stack docker container

# Create a docker-compose.yaml placed in root dir with following description
# Then just run >> docker compose up

version: "3.9"
services:
  redis:
    image: "redis/redis-stack:edge"
    ports:
      - "6379:6379"
    environment:
      - "REDIS_ARGS=--appendonly yes"
    volumes:
      - ./data:/data
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
Pisa answered 13/11, 2023 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.