Skip to content

Next.js TypeError: Cannot read properties of undefined (reading 'bind') - caused by middleware.ts #56368

@mstys

Description

@mstys

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64
Binaries:
  Node: 16.13.1
  npm: 8.19.2
  Yarn: 1.22.11
  pnpm: N/A
Relevant Packages:
  next: 13.5.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: standalone

Which example does this report relate to?

https://github.com/vercel/next.js/blob/canary/examples/with-docker/README.md

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

I getting following error on run next app after npm run build

(node:50480) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 ⚠ "next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.
  ▲ Next.js 13.5.4
  - Local:        http://localhost:3000

 ✓ Ready in 257ms
TypeError: Cannot read properties of undefined (reading 'bind')
    at NextNodeServer.handleRequestImpl (/Users/michal.stys/OwnProjects/next13-test/node_modules/next/dist/server/base-server.js:389:50)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
TypeError: Cannot read properties of undefined (reading 'bind')
    at NextNodeServer.handleRequestImpl (/Users/michal.stys/OwnProjects/next13-test/node_modules/next/dist/server/base-server.js:389:50)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

My middleware.ts from next.js docs.

import { NextRequest, NextResponse } from "next/server";

const PUBLIC_FILE = /\.(.*)$/;

export async function middleware(req: NextRequest) {
  if (
    req.nextUrl.pathname.startsWith("/_next") ||
    req.nextUrl.pathname.includes("/api/") ||
    PUBLIC_FILE.test(req.nextUrl.pathname)
  ) {
    return;
  }

  if (req.nextUrl.locale === "default") {
    const locale = "en";
    return NextResponse.redirect(
      new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url)
    );
  }
}

PS. all works perfect without this middleware

Expected Behavior

Type error doesn't exists if using middleware.ts

To Reproduce

npx create-next-app --example with-docker nextjs-docker

// add middleware.ts added above

npm run build
npm start

NEXT-1739

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.linear: nextConfirmed issue that is tracked by the Next.js team.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions