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-scriptVerified import paths — ran on the pinned version, not inferred.
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.
Always ensure the `numKeys` parameter accurately reflects the number of elements in your `KEYS` array within the Lua script.
Upgrade your Node.js environment to version 10 or newer to ensure compatibility and proper function execution.
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()`.
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.
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`.