Nest Router Module
nest-router is a module for the NestJS framework designed to provide a hierarchical and modular approach to route organization. It allows developers to define a tree-like structure for application routes, automatically prefixing controller paths based on parent module configurations. For example, a child module's routes will inherit the parent's prefix, resulting in `/parent/child/route` rather than just `/child/route`. This addresses a common routing organization challenge in older NestJS versions (specifically prior to features like `APP_BASE_URL` or later routing enhancements). The last stable version is 1.0.9, published 7 years ago. This package is largely incompatible with modern NestJS versions (v6+ and especially v10+, with NestJS 11.1.19 being the latest as of April 2026). Its primary differentiator was its ability to structure routes in a more organized, tree-like manner for NestJS applications relying on a specific routing pattern.
Common errors
-
Nest can't resolve dependencies of the ApplicationModule (?). Please make sure that the argument RouterModule is available in the current context.
cause The `RouterModule` or its dependencies were not properly imported or provided within the NestJS module.fixEnsure `RouterModule` is correctly imported via `import { RouterModule } from 'nest-router';` and `RouterModule.forRoutes(routes)` is added to the `imports` array of your root module (e.g., `ApplicationModule`). Also, ensure all modules specified in your `Routes` array are also imported directly into the parent module importing `RouterModule.forRoutes`. -
TypeError: Cannot read properties of undefined (reading 'forRoutes')
cause Attempting to call `forRoutes` on `RouterModule` before `RouterModule` itself has been correctly imported or when the imported symbol is undefined.fixVerify the import statement `import { RouterModule } from 'nest-router';` is present and correct. This typically happens if `RouterModule` is not imported as a named export. -
Error: Nest can't resolve dependencies of the SomeMiddleware (?). Please make sure that the argument SomeController is available in the current context.
cause Middleware is being applied to a controller using `forRoutes(SomeController)` without considering `nest-router`'s path resolution, leading to an incorrect path lookup.fixWhen using `nest-router`, apply middleware using `forRoutes(RouterModule.resolvePath(SomeController))` to ensure NestJS uses the full, hierarchically prefixed path for the controller.
Warnings
- breaking The `nest-router` package is designed for NestJS versions greater than 4.5.10. It is largely incompatible with modern NestJS versions (v6+ and especially v10+). Attempting to use this package with current NestJS releases will likely result in compilation errors or runtime failures due to significant API changes in the core framework over the past several years.
- breaking This package has been abandoned for over 7 years. It receives no updates, bug fixes, or security patches, making it a potential security vulnerability and functionally obsolete for modern development practices. Relying on it is strongly discouraged.
- gotcha When defining nested routes, child module controllers will be prefixed by the full path of their parent and grandparent modules, not just their immediate parent. For example, a controller in `CatsModule` (child of `NinjaModule` with path `/ninja`) will resolve to `/ninja/cats/controller-path`, not `/cats/controller-path`.
- gotcha NestJS middleware often requires the full resolved path of a controller, which `nest-router`'s module prefixes affect. Standard `forRoutes(SomeController)` in middleware may not work as expected.
Install
-
npm install nest-router -
yarn add nest-router -
pnpm add nest-router
Imports
- RouterModule
const RouterModule = require('nest-router');import { RouterModule } from 'nest-router'; - Routes
import Routes from 'nest-router';
import { Routes } from 'nest-router'; - forRoutes
new RouterModule().forRoutes(routes);
RouterModule.forRoutes(routes);
- resolvePath
new RouterModule().resolvePath(SomeController);
RouterModule.resolvePath(SomeController);
Quickstart
import { Module } from '@nestjs/common';
import { RouterModule, Routes } from 'nest-router';
// Imagine these are actual NestJS modules with controllers
class CatsModule {}
class DogsModule {}
class NinjaModule {}
const routes: Routes = [
{
path: '/ninja',
module: NinjaModule,
children: [
{
path: '/cats',
module: CatsModule,
},
{
path: '/dogs',
module: DogsModule,
},
],
},
];
@Module({
imports: [
RouterModule.forRoutes(routes), // Set up the routes hierarchy
CatsModule,
DogsModule,
NinjaModule // All modules must also be imported normally
],
})
export class ApplicationModule {}
// Example of how a controller path would resolve:
// A controller in CatsModule with @Controller('my-cat')
// would have a full path of /ninja/cats/my-cat