Registry / database / node-redis-script

node-redis-script

JSON →
library2.0.1jsnpmunverified

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.

npm install node-redis-script
INSTALL
IMPORT
SIG · NODE-REDIS-SCRIPT
N
node-redis-script
databasejavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createScript
import { createScript } from 'node-redis-script';
const createScript = require('node-redis-script').createScript;
ESM import for `createScript`. While the README shows CommonJS, modern Node.js environments typically use ESM. The library ships with CJS for Node 10+.
createScript
const { createScript } = require('node-redis-script');
import { createScript } from 'node-redis-script';
CommonJS import for `createScript`, matching the documentation example. This is suitable for older Node.js versions or CJS modules.
* as RedisScript
import * as RedisScript from 'node-redis-script';
import RedisScript from 'node-redis-script';
Importing all named exports as a namespace, useful if multiple utilities were exposed. There is no default export.

This example demonstrates how to initialize a Redis client, define a Lua script, create a script function using `createScript`, and then execute it with specific keys and arguments. It showcases a common pattern for an atomic increment-and-expire operation.

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);
Debug
Known issues
gotchaThe `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.
fix
Always ensure the `numKeys` parameter accurately reflects the number of elements in your `KEYS` array within the Lua script.
affects: >=1.0.0
breakingThis library requires Node.js version 10 or higher. Older Node.js versions are not supported.
fix
Upgrade your Node.js environment to version 10 or newer to ensure compatibility and proper function execution.
affects: <=1.x
gotchaThe `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.
fix
Ensure your Redis client (either `node-redis` or `ioredis`) is fully connected and ready before passing it to `createScript`. For `node-redis` v4+, remember to call `.connect()`.
affects: >=1.0.0
Errors
Common errors & fixes
ERR wrong number of arguments for 'eval' command
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.
fix
Review 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')
The `redis` or `ioredis` client object passed to `createScript` is `undefined`, `null`, or an improperly initialized object lacking the `eval` or `evalsha` methods.
fix
Verify that you are passing a valid, connected instance of either `node-redis`'s client or `ioredis`'s client to the `opts` object for `createScript`.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
node-redisoptionalRequired as a Redis client for communication; version 0.10+ is needed.
ioredisoptionalAlternative Redis client for communication; also supported.
Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
node-redis-script — npm install node-redis-script · libregistry