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.

Can't build Next.js app with Middleware

See original GitHub issue

Current Behavior

info  - Checking validity of types  
warn  - using beta Middleware (not covered by semver) - https://nextjs.org/docs/messages/beta-middleware
info  - Creating an optimized production build  
Failed to compile.

../../node_modules/next/dist/compiled/regenerator-runtime/runtime.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware pages/_middleware

Import trace for requested module:
./pages/_middleware.page.ts

Build failed because of webpack errors

 β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”

 >  NX   Ran target build for project app (12s)
 
    βœ–    1/1 failed
    βœ”    0/1 succeeded [0 read from cache]

Expected Behavior

I expect the app to build successfully. I am trying to convert an existing Next.js app. It worked before using @nrwl/next:build.

Steps to Reproduce

Add a _middleware.ts file:

export async function middleware() {
  console.log("test");
}

I’m experiencing the exact same issue as: https://github.com/vercel/next.js/issues/35343

Failure Logs

See above.

Environment


 >  NX   Report complete - copy this into the issue template

   Node : 14.17.0
   OS   : darwin x64
   yarn : 1.19.0
   
   nx : 13.9.5
   @nrwl/angular : Not Found
   @nrwl/cypress : 13.9.5
   @nrwl/detox : Not Found
   @nrwl/devkit : 13.9.5
   @nrwl/eslint-plugin-nx : 13.9.5
   @nrwl/express : Not Found
   @nrwl/jest : 13.9.5
   @nrwl/js : 13.9.5
   @nrwl/linter : 13.9.5
   @nrwl/nest : Not Found
   @nrwl/next : 13.9.5
   @nrwl/node : Not Found
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : 13.9.5
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 13.9.5
   @nrwl/web : 13.9.5
   @nrwl/workspace : 13.9.5
   typescript : 4.6.3
   rxjs : 6.6.7
   ---------------------------------------
   Community plugins:

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
tmilewskicommented, May 2, 2022

Just wanted to follow up here:

There was a related issue filed on the NextJS repo: https://github.com/vercel/next.js/issues/35973

Per my experience:

Importing from a middleware-specific project: On serve: Success On build: ../../node_modules/next/dist/compiled/regenerator-runtime/runtime.js error.

Placing all code from said middleware-specific project directly into the _middleware.ts file: On serve: Success On build: Success

2reactions
jberrydevcommented, Mar 31, 2022

Also getting this error when using middleware. It appears to be around importing from an NX lib inside of nextjs _middleware file. You will also notice if you were to import your middleware function from a lib you may get Cannot find module export. Seems to be something related to how the _middleware file is handled within NX.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Middleware - Next.js
Middleware allows you to run code before a request is completed, then based on the incoming request, you can modify the response by...
Read more >
Why am I getting this error when using Next.js middleware?
Prior to creating this file, the app worked perfectly. I could authentiate via an email link, make simple GET requests to API routes,...
Read more >
next-connect - npm
The method routing and middleware layer for Next.js (and many others). ... TypeScript icon, indicating that this package has built-in typeΒ ...
Read more >
Edge Middleware Quickstart – Vercel Docs
Create Edge Middleware using Next.js. Before you begin, ensure that your app is using the most recent version of Next.js:.
Read more >
How to Add New Middleware Feature with Next.js ... - YouTube
How to Add New Middleware Feature with Next. js 12.2 In Application - For BeginnersHey everyone welcome back to daily tuition.
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