NashTech Insights

Exploring NestJS Modules

Table of Contents


NestJS, a progressive Node.js framework, provides a solid foundation for developing robust server-side applications. One of the key features that make NestJS powerful and flexible is its module system. In this blog post, we will dive into NestJS modules and explore how they can help you structure your application and build highly modular and extensible codebases.

What are NestJS Modules?

In Nest.js, a module is a specific piece of code that brings together related components, services, and controllers. It promotes code organization, separation of concerns, and reusability, making it easier to maintain and scale applications.

Each application has at least one module, a root module. The root module is the starting point Nest uses to build the application graph.

A module is a class decorated with @Module annotation. Each module contains a set of providers, controllers, and other related artifacts.

Module Metadata

  1. imports: This set of array specifies the modules that are required by the current module. Ex:- built-in modules, custom modules, third-party modules etc.
  2. exports: It determines which components, services, or other artifacts should be accessible to other modules when they import the current module.
  3. controllers: Declares the controllers associated with the current module. Controllers handle incoming requests and define the application’s routes.
  4. providers: Registers the services, repositories, or other providers that the module offers. Providers are typically responsible for handling business logic and providing data to other components.

Creating a module

To create a new module in NestJS, you can use the CLI command nest g module <module_name>, which automatically generates the required boilerplate code. For Example:

nest g module users

Feature Modules

A featured module refers to a module that contains a specific feature or functionality within an application. It contains the necessary components, controllers, services, and providers required to implement a specific feature. The directory structure will look like:

Let’s understand this with an example:

In the above example, we defined a UsersModule to maintain user specific features. All we need to do is to import the UserModule in root module(AppModule).

Shared Modules

In NestJS, every module is automatically a shared module. Once created it can be reused by any module.

Let’s suppose we want to use an instance of UsersService on any other module, to achieve this all we need to do that to export the UsersService from UsersModule and import the UsersModule in the required modules. To export, add UsersService in export array as given below:

Global Modules

Global modules are modules that are available and accessible throughout the entire application. The @Global() decorator makes the module global-scoped.

In the above example, we defined the UsersModules as global module. Now if, any modules in the application wish to inject the UsersService, will not require to import UsersModule.


Nest.js modules are a powerful mechanism for organising and scaling your applications. In this blog post, we explored t how modules help to maintain a clear structure, including how to create modules, import them into other modules and export functionality.

For more such posts, please follow our LinkedIn page- FrontEnd Competency.



Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: