question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make global module as an option

See original GitHub issue

Hi! 👋

Firstly, thanks for your work on this project! 🙂

I would like to set up the redis module in a non-global way, so is there a chance we can make it configurable? Thanks!

Here is the diff that solved my problem:

diff --git a/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js b/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js
index 8d6de0a..d72312e 100644
--- a/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js
+++ b/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js
@@ -29,7 +29,7 @@ let RedisModule = RedisModule_1 = class RedisModule {
             ...redisClientProviders
         ];
         return {
-            global: true,
+            global: false,
             module: RedisModule_1,
             providers,
             exports: [redis_manager_1.RedisManager, ...redisClientProviders]

This issue body was partially generated by patch-package.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
liaoliaotscommented, Feb 21, 2022

please follow the steps below:

  1. bump @liaoliaots/nestjs-redis to 6.0.0
npm install --save @liaoliaots/nestjs-redis@6
  1. create a shared module:
// shared-redis.module.ts

import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';

@Module({
    imports: [
        RedisModule.forRoot(
            {
                readyLog: true,
                config: {
                    host: '127.0.0.1',
                    port: 6380,
                    password: 'bitnami'
                }
            },
            false // <--- make the module non global
        )
    ],
    exports: [RedisModule] // <--- important: share this module
})
export class SharedRedis {}
  1. import SharedRedis to your business module:
// cats.module.ts

import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';
import { SharedRedis } from '../shared-redis.module';

@Module({
    imports: [SharedRedis], // <---
    providers: [CatsService],
    controllers: [CatsController]
})
export class CatsModule {}
  1. test
npm run start:dev
1reaction
liaoliaotscommented, Mar 2, 2022

@AvantaR It may be useful for you to import files in the correct order:

As mentioned above, decorator @InjectRedis is used in CatsService. If import SharedRedis before CatsService, nest will throw an error, as shown below. Because some methods in RedisModule will get called after the @InjectRedis calls and therefore RedisModule will create/re-export providers correctly.

Screenshot_20220302_233757

If import CatsService first, it works as expected:

Screenshot_20220302_233830

- - OR - - You can use RedisModule in the global scope, that error will not exist.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make changes to a global module in only one template
To make a change to your global module that will only affect one page, go to your template and make a local copy...
Read more >
How to programmatically set a global (module) variable?
Here is a better way to do it: import sys definitions = {'a': 1, 'b': 2, 'c': 123.4} module = sys.modules[__name__] for name, ......
Read more >
Documentation - Global: Modifying Module - TypeScript
A global-modifying module alters existing values in the global scope when they are imported. For example, there might exist a library which adds...
Read more >
Adding Global Modules <add> - Microsoft Learn
Optional string attribute. Specifies conditions under which the global module will run. The preCondition attribute can have one or more of the ...
Read more >
What is a Divi Global Module and How to Create One?
Give your global module a name and tick the Make this a global item option and click the Save to Library button. Global...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found