Skip to content

11.0.0 Global Modules middleware execution order #14487

Closed
@tsangste

Description

@tsangste

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Prior to NestJS 11, middleware registered on a global module was resolved and executed based on its distance from the module, as noted in #11133 (comment). However, since version 11, with the change 6a84d6e all middleware registered on a global module is now executed last.

Minimum reproduction code

https://github.com/tsangste/nestjs-middleware-order

Steps to reproduce

  1. Pull repo
  2. run npm i
  3. run npm start
  4. curl GET to http://localhost:3000/local

Expected behavior

I would expect GlobalMiddleware to log first before LocalMiddleware

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

11.0.0

Packages versions

Node.js version

22.13.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

Current log output
Image

Could you clarify whether this change represents the new intended behaviour that middleware registered on a global module will always execute last or if this is a bug?

Previously, I was addressing a bug within the NestJS module for mikro-orm (mikro-orm/nestjs#189) to ensure the middleware executed first, allowing it to set up its request context. However, after the changes introduced in NestJS 11, this PR started failing, which is how I discovered the issue.

If this is the intended behaviour moving forward, could you suggest a way to work around it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions