cnorris
cnorris

Reputation: 548

How to organise dependent modules in a re-usable way?

I currently have two modules:

Auth requires the User module to register and validate users. So currently, Auth imports the User module:

@Module({
    imports: [UserModule],
    components: [JwtStrategy, GithubStrategy, TokenService, LoginService],
    controllers: [AuthController],
})

However, as a result if I ever wanted to re-use these modules, I'd have to include both. Furthermore, if I moved the User module, I'd have to update all the references to it within the Auth module. Is there a better way to organise two dependent modules? Should they be combined?

Is there a better way to do this?

Upvotes: 0

Views: 160

Answers (1)

Shady Khalifa
Shady Khalifa

Reputation: 187

If your UserModule will be used everywhere, say in OrdersModules, NotificationsModule..., and you found yourself importing it and reimporting everywhere, then you should take a step to make this Module a GlobalModule, then it will be available everywhere in your project since it imported in RootModule i.e ApplicationModule

Just annotate your Module by @Global

Example

See also: Modules | Nest Documentations

Upvotes: 1

Related Questions