最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nestjs - Nx not able to build with shared middleware lib - Stack Overflow

programmeradmin1浏览0评论

I'm working with NX + NestJs. This is my app repo structure:

MyRepo
    -> apps
        -> NestJsApp1
        -> App2
    ->lib
        -> middlewares
            -> LoggerMiddleware

I have create Nestjs apps using @nx/nest:app NestJsApp1, and middleware libs using @nx/node:lib libs/middleware This is the Logger Middleware.

@Injectable()
export class LoggingMiddleware implements NestMiddleware {
    use(req: Request, res: Response, next: NextFunction) {
        console.log(`LoggingMiddleware: ${req.method} ${req.url}`);
        next();
    }
}

This is my NestJSApp1 => AppModule.ts

import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { LoggingMiddleware } from 'middlewares';

@Module({
    imports: [],
    controllers: [],
    providers: [],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer
            .apply(LoggingMiddleware)
            .forRoutes('*'); // Apply to all routes
    }
}

When i server my app, it is working fine. LoggerMiddleware is working properly. However, when I build my app using command nx run app1:build, the app is build without any issue, when then i try to run the main.js from dist, it is throwing error that Middleware Module cannot be found. I looked into the appModule in dist. This is the build structure:

dist
    -> apps
        -> app1
            -> src
                -> appModules.js
                -> all other js

    -> lib
        -> middlewares
            -> src 
                -> index.js

Inside appModule, the middleware is being imported:

var import_middlewares = require("middlewares");

However, if i manually change the path to point the middlewares, it is working.

var import_middlewares = require("../../../libs/middlewares/src");

So any reason why the build is not able to point to the lib correctly ?

发布评论

评论列表(0)

  1. 暂无评论