question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use the middleware feature with React 18 & output:standalone feature

See original GitHub issue

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #137-Ubuntu SMP Wed Jun 15 13:33:07 UTC 2022
Binaries:
  Node: 17.8.0
  npm: 8.5.5
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 12.2.3
  eslint-config-next: 12.2.3
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

standalone / server.js / docker

Describe the Bug

It’s currently impossible to use the middleware feature with React18 and the output:standalone feature. Trying to force the experimental.runtime:nodejs has no effect.

The simplest middleware, like a more complicated, result to the same issue.

import { NextResponse } from 'next/server'

export const middleware = async (request) => {
  console.log(request)
}

Real example:

/* eslint-disable @next/next/no-server-import-in-page */
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

import { COOKIE_JWT_HP } from '@/services/auth'
import { routes } from '@/utils/route'

export const middleware = async (request: NextRequest) => {
  if (request.nextUrl.pathname === '/') {
    if (!request.cookies.get(COOKIE_JWT_HP)) {
      return
    }

    const url = request.nextUrl.clone()
    url.pathname = routes.dashboard
    return NextResponse.redirect(url)
  }
}

export const config = {
  matcher: ['/'],
}

The error:

Error: Cannot find module 'next/dist/compiled/@edge-runtime/primitives/console'
Require stack:
- /app/node_modules/next/dist/compiled/edge-runtime/index.js
- /app/node_modules/next/dist/server/web/sandbox/context.js
- /app/node_modules/next/dist/server/web/sandbox/sandbox.js
- /app/node_modules/next/dist/server/web/sandbox/index.js
- /app/node_modules/next/dist/server/next-server.js
- /app/server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at addPrimitives (file:///app/node_modules/next/dist/compiled/edge-runtime/index.js:1:983)
    at Object.extend (file:///app/node_modules/next/dist/compiled/edge-runtime/index.js:1:388)
    at new VM (file:///app/node_modules/next/dist/compiled/edge-runtime/index.js:1:7886)
    at new EdgeVM (file:///app/node_modules/next/dist/compiled/edge-runtime/index.js:1:348)
    at new EdgeRuntime (file:///app/node_modules/next/dist/compiled/edge-runtime/index.js:1:8590)
    at createModuleContext (file:///app/node_modules/next/dist/server/web/sandbox/context.js:71:21)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.getModuleContext (file:///app/node_modules/next/dist/server/web/sandbox/context.js:41:25)

Expected Behavior

No error.

Link to reproduction

https://github.com/webda2l/nextjs-issue1

To Reproduce

Build the docker, and run it http://localhost:3000/

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:14
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
ref-thomasledoux1commented, Jul 25, 2022

It seems like the “next/dist/compiled/@edge-runtime” dependency is not being picked up correctly by the tracing feature used by the outputstandalone… We added the following to our catch-all route (pages/[[…slug]]):

export const config = {
  unstable_includeFiles: [
    'node_modules/next/dist/compiled/@edge-runtime/primitives/**/*.+(js|json)',
  ],
}

This seems to have done the trick for now.

4reactions
ref-thomasledoux1commented, Jul 25, 2022

Good to hear! Hope this gets picked up by the team to fix it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Output File Tracing - Next.js
Next.js automatically traces which files are needed by each page to allow for easy deployment of your application. Learn how it works here....
Read more >
I am getting error while converting my next js project to docker
Just change nextjs version in package.json file to latest and run yarn install . Now docker build work fine. Share.
Read more >
Next.js 12 Has Been Released - Morioh
To use Middleware in Next.js, you can create a file pages/_middleware.js . ... Concurrent features in React 18 include built-in support for server-side ......
Read more >
React v18.0 – React Blog
The new rendering behavior in React 18 is only enabled in the parts of your app that use new features. The overall upgrade...
Read more >
React 18, React Redux 8, and TypeScript: What you need to ...
With React 18 currently in the works, React Redux 8, the React state ... to incorporate all the new features that React 18...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found