Can't build Next.js app with Middleware
See original GitHub issueCurrent 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:
- Created a year ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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.