{"id":16147,"library":"node-redis-script","title":"Redis Script Utility for Node.js","description":"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.","status":"maintenance","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/thedeveloper/scripty","tags":["javascript","redis","script","node.js","manager"],"install":[{"cmd":"npm install node-redis-script","lang":"bash","label":"npm"},{"cmd":"yarn add node-redis-script","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-redis-script","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a Redis client for communication; version 0.10+ is needed.","package":"node-redis","optional":true},{"reason":"Alternative Redis client for communication; also supported.","package":"ioredis","optional":true}],"imports":[{"note":"ESM import for `createScript`. While the README shows CommonJS, modern Node.js environments typically use ESM. The library ships with CJS for Node 10+.","wrong":"const createScript = require('node-redis-script').createScript;","symbol":"createScript","correct":"import { createScript } from 'node-redis-script';"},{"note":"CommonJS import for `createScript`, matching the documentation example. This is suitable for older Node.js versions or CJS modules.","wrong":"import { createScript } from 'node-redis-script';","symbol":"createScript","correct":"const { createScript } = require('node-redis-script');"},{"note":"Importing all named exports as a namespace, useful if multiple utilities were exposed. There is no default export.","wrong":"import RedisScript from 'node-redis-script';","symbol":"* as RedisScript","correct":"import * as RedisScript from 'node-redis-script';"}],"quickstart":{"code":"import { createClient } from 'redis'; // or from 'ioredis'\nimport { createScript } from 'node-redis-script';\n\nasync function runRedisScript() {\n  const redisClient = createClient();\n  redisClient.on('error', (err) => console.log('Redis Client Error', err));\n  await redisClient.connect();\n\n  const incrbyExSrc = `\n    local current\n    current = redis.call('incrby',KEYS[1],ARGV[1])\n    redis.call('expire',KEYS[1],ARGV[2]);\n    return current\n  `;\n\n  // give it a redis client and script source\n  const opts = { redis: redisClient }; // For ioredis: { ioredis: ioredisClient }\n  const incrbyEx = createScript(opts, incrbyExSrc);\n\n  // redis requires you to tell it how many keys to expect\n  const numKeys = 1;\n  const key = 'myCounterKey';\n  const incr = 5;\n  const ex = 60; // expire in 60 seconds\n\n  const result = await incrbyEx(numKeys, key, incr, ex);\n  console.log(`Current value of ${key}: ${result}`); // Should print 5 the first time, 10 the second, etc.\n\n  await redisClient.disconnect();\n}\n\nrunRedisScript().catch(console.error);","lang":"javascript","description":"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."},"warnings":[{"fix":"Always ensure the `numKeys` parameter accurately reflects the number of elements in your `KEYS` array within the Lua script.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade your Node.js environment to version 10 or newer to ensure compatibility and proper function execution.","message":"This library requires Node.js version 10 or higher. Older Node.js versions are not supported.","severity":"breaking","affected_versions":"<=1.x"},{"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()`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","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.","error":"ERR wrong number of arguments for 'eval' command"},{"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`.","cause":"The `redis` or `ioredis` client object passed to `createScript` is `undefined`, `null`, or an improperly initialized object lacking the `eval` or `evalsha` methods.","error":"TypeError: Cannot read properties of undefined (reading 'eval')"}],"ecosystem":"npm"}