Redis Script Utility for Node.js
node-redis-script is a JavaScript library designed to simplify the execution of Lua scripts within Redis from Node.js applications. It abstracts the complexities of `EVAL` and `EVALSHA` commands, allowing developers to define scripts as strings and then execute them via a generated function. The current stable version is 2.0.1. It primarily focuses on providing a clean, function-oriented API for script management rather than requiring direct interaction with Redis `EVAL` commands. This library automatically handles script loading and caching using `EVALSHA` for efficiency, reducing network overhead after the initial script load. It offers flexibility by supporting both `node-redis` (v0.10+) and `ioredis` clients, allowing integration with existing Redis client setups.
Common errors
-
ERR wrong number of arguments for 'eval' command
cause The `numKeys` parameter passed to your generated script function does not match the actual number of keys used in the Lua script's `KEYS` array.fixReview your Lua script to count how many `KEYS[n]` variables are accessed, and ensure the `numKeys` argument exactly matches this count when calling your script function. -
TypeError: Cannot read properties of undefined (reading 'eval')
cause The `redis` or `ioredis` client object passed to `createScript` is `undefined`, `null`, or an improperly initialized object lacking the `eval` or `evalsha` methods.fixVerify that you are passing a valid, connected instance of either `node-redis`'s client or `ioredis`'s client to the `opts` object for `createScript`.
Warnings
- gotcha The `numKeys` argument passed to the generated script function is critical and directly corresponds to Redis's `EVAL` command. Incorrectly specifying the number of keys will lead to runtime errors in Redis.
- breaking This library requires Node.js version 10 or higher. Older Node.js versions are not supported.
- gotcha The `redis` or `ioredis` client provided to `createScript` must be a properly initialized and connected client instance. Passing an unconnected or invalid client will result in execution failures.
Install
-
npm install node-redis-script -
yarn add node-redis-script -
pnpm add node-redis-script
Imports
- createScript
const createScript = require('node-redis-script').createScript;import { createScript } from 'node-redis-script'; - createScript
import { createScript } from 'node-redis-script';const { createScript } = require('node-redis-script'); - * as RedisScript
import RedisScript from 'node-redis-script';
import * as RedisScript from 'node-redis-script';
Quickstart
import { createClient } from 'redis'; // or from 'ioredis'
import { createScript } from 'node-redis-script';
async function runRedisScript() {
const redisClient = createClient();
redisClient.on('error', (err) => console.log('Redis Client Error', err));
await redisClient.connect();
const incrbyExSrc = `
local current
current = redis.call('incrby',KEYS[1],ARGV[1])
redis.call('expire',KEYS[1],ARGV[2]);
return current
`;
// give it a redis client and script source
const opts = { redis: redisClient }; // For ioredis: { ioredis: ioredisClient }
const incrbyEx = createScript(opts, incrbyExSrc);
// redis requires you to tell it how many keys to expect
const numKeys = 1;
const key = 'myCounterKey';
const incr = 5;
const ex = 60; // expire in 60 seconds
const result = await incrbyEx(numKeys, key, incr, ex);
console.log(`Current value of ${key}: ${result}`); // Should print 5 the first time, 10 the second, etc.
await redisClient.disconnect();
}
runRedisScript().catch(console.error);