Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: client.evalsha is not a function #286

Open
epubreader opened this issue Nov 6, 2023 · 3 comments
Open

TypeError: client.evalsha is not a function #286

epubreader opened this issue Nov 6, 2023 · 3 comments

Comments

@epubreader
Copy link

did not support node-redis,
const redis = require("redis");

TypeError: client.evalsha is not a function
at Redlock._attemptOperationOnClient (/home/mason/project/epubpub/epub-search/node_modules/redlock/dist/cjs/index.js:362:49)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

const redisClient = require("./redisClient");
const { default: Redlock, ResourceLockedError } = require("redlock");

const redLock = new Redlock([redisClient], {
// the expected clock drift; for more details
// see http://redis.io/topics/distlock
driftFactor: 0.01, // time in ms

// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 4,

// the time in ms between attempts
retryDelay: 50, // time in ms

// the max time in ms randomly added to retries
// to improve performance under high contention
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
retryJitter: 25, // time in ms
});

redLock.on("error", (error) => {
// Ignore cases where a resource is explicitly marked as locked on a client.
if (error instanceof ResourceLockedError) {
return;
}

// Log all other errors.
console.error(error);
});

module.exports = redLock;

const config = require("../config");

const redis = require("redis");
const redisPort = config.redis.port;
const redisHost = config.redis.host;
const redisPass = config.redis.password;

let redisConfig = {
socket: {
host: redisHost,
port: redisPort,
},
password: redisPass,
};

const redisClient = redis.createClient(redisConfig);

redisClient.on("error", (err) => {
console.error("Error: %o ", err);
});

redisClient.on("connect", () => {
console.log("Connected to Redis");
});

redisClient.on("close", () => {
console.log("Connection to redis has closed");
});

redisClient.connect();

module.exports = redisClient;

@priyadarshy
Copy link

I was expecting that using node-redis would work but I fixed this by swapping to the ioredis package.

@jopealgorta
Copy link

Hi, the problem is that older versions of redis supported all-lower-cased methods, but newer versions switched to camel casing. You should patch your redis client. Something like:

client = createClient({ url: `redis://${REDIS_HOST}:${REDIS_PORT}` });
if (!(client as any).evalsha) {
  (client as any).evalsha = client.evalSha.bind(client);
}

credits: @gfzabarino

@junereycasuga
Copy link

Hi, the problem is that older versions of redis supported all-lower-cased methods, but newer versions switched to camel casing. You should patch your redis client. Something like:

client = createClient({ url: `redis://${REDIS_HOST}:${REDIS_PORT}` });
if (!(client as any).evalsha) {
  (client as any).evalsha = client.evalSha.bind(client);
}

credits: @gfzabarino

this didn't work for us. we got a different error after doing this.

/projects/distlock/node_modules/@redis/client/dist/lib/commands/generic-transformers.js:248
        args.push(options.keys.length.toString(), ...options.keys);

redis: 4.6.14
redlock: 4.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants