Skip to content

req.query is now a getter and can't use as custom server in front of Next.js #6633

Closed
vercel/next.js
#81573

Description

@ale-grosselle

When using Express as a custom server in front of Next.js, accessing req.query directly no longer returns a mutable plain object. Instead, it is now a getter, and attempts to modify it or use it as a plain object lead to unexpected behavior or errors.

This change affects routes where the server modifies or depends on the mutability of req.query before passing it to Next.js, breaking compatibility with existing implementations.

Next.js Issue:
vercel/next.js#79158


Context

We applied the following patch, but we do not believe it is the best solution:

// Express 5 makes the 'query' property read-only, but Next.js needs to write to it
try {
  Object.defineProperty(req, 'query', descriptor);
} catch (error) {
  // If property already exists and is configurable, delete and recreate it
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    delete (req as any).query;
    Object.defineProperty(req, 'query', descriptor);
    logger.logInfo(`query property fixed ${error}`, requestID);
  } catch (error) {
    logger.logWarn(
      `Could not fix query property for Next.js compatibility: ${error}`,
      requestID
    );
  }
}

Is it possible to know why Express decided to change? This would help us understand where we need to make adjustments (on the Next.js side or the Express.js side).

Activity

  1. bjohansebas commented on Jul 13, 2025

    @bjohansebas
    Member

    While I wasn't around at the time when that commit was made and possibly discussed, I found reasons (#3472 (comment), #2752 (comment)) from the former maintainer for making this just a getter.

    The long-term solution is to use req.locals to store the query and modify it later, i'm not sure how this could work for Next.js, #3472 (comment)

  2. locked and limited conversation to collaborators on Jul 13, 2025
  3. converted this issue into a discussion #6637 on Jul 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions