Registry / storage / nestjs-simple-redis-lock

nestjs-simple-redis-lock

JSON →
library1.0.1jsnpmunverified

A distributed locking library for NestJS using a single Redis instance. Version 1.0.1 provides a simple lock/unlock API and a decorator-based approach for automatic locking around method execution. It offers configurable lock duration, retry interval, and max retries. Requires the 'nestjs-redis' package as a peer dependency and integrates with NestJS's module system. Compared to alternatives like 'redis-semaphore' or 'redlock', this package focuses on simplicity and tight NestJS integration with minimal configuration.

npm install nestjs-simple-redis-lock
INSTALL
IMPORT
SIG · NESTJS-SIMPLE-REDI
N
nestjs-simple-redis-lock
storagejavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

RedisLockModule
import { RedisLockModule } from 'nestjs-simple-redis-lock';
const RedisLockModule = require('nestjs-simple-redis-lock').RedisLockModule;
ESM-only; CommonJS require works but destructure
RedisLockService
import { RedisLockService } from 'nestjs-simple-redis-lock';
import * as RedisLockService from 'nestjs-simple-redis-lock';
Named export, not default
RedisLock
import { RedisLock } from 'nestjs-simple-redis-lock';
import RedisLock from 'nestjs-simple-redis-lock';
Decorator is a named export

Shows importing RedisLockModule, injecting RedisLockService, and using lock/unlock in a service.

import { Module } from '@nestjs/common'; import { RedisModule } from 'nestjs-redis'; import { RedisLockModule, RedisLockService } from 'nestjs-simple-redis-lock'; @Module({ imports: [ RedisModule.forRoot({ host: process.env.REDIS_HOST ?? 'localhost', port: parseInt(process.env.REDIS_PORT ?? '6379'), }), RedisLockModule.register({}), ], providers: [ { provide: 'MyService', useClass: class MyService { constructor(private readonly lockService: RedisLockService) {} async doSomething() { await this.lockService.lock('my-lock'); try { // critical section } finally { await this.lockService.unlock('my-lock'); } } }, }, ], }) export class AppModule {}
Debug
Known issues
gotchaMust import RedisModule before RedisLockModule in the same module, otherwise Redis connection not available.
fix
Ensure RedisModule.forRoot/forAsync is imported before RedisLockModule.register.
affects: >=1.0.0
gotchalock() returns void, not a promise of a lock object. Unlocking must be done manually in finally block.
fix
Call await this.lockService.unlock('name') after critical section.
affects: >=1.0.0
deprecatedAll methods are sync except unlock (which returns Promise<void>). Do not await lock() or setTTL().
fix
Remove await from lock() and setTTL() calls.
affects: >=1.0.0
gotchaDecorator @RedisLock uses the first argument as lock name function; if a string is passed, it's used directly. But if the method returns a value, the decorator will also return it.
fix
Ensure the decorated method returns the desired value.
affects: >=1.0.0
Errors
Common errors & fixes
Nest can't resolve dependencies of RedisLockService (?). Please make sure that the argument RedisService at index [0] is available in the RedisLockModule context.
RedisLockModule registered before RedisModule, or RedisModule not imported at all.
fix
Import RedisModule before RedisLockModule in the same module imports array.
Cannot read properties of undefined (reading 'lock')
RedisLockService not injected properly or RedisModule not configured.
fix
Use constructor injection and ensure RedisLockModule.register is called.
TypeError: this.lockService.unlock is not a function
Used CommonJS require incorrectly or imported wrong export.
fix
Use 'import { RedisLockService } from 'nestjs-simple-redis-lock';'
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
nestjs-redisrequiredpeer dependency required for Redis connection; the lock service uses this package internally
Agent activity
22 hits · last 30 days
node
20
Amazon
1
OpenAI (training)
1
Resources
nestjs-simple-redis-lock — npm install nestjs-simple-redis-lock · libregistry