`eval` not allowed in Middleware pages/foo/_middleware
See original GitHub issueWhat version of Next.js are you using?
12.0.1
What version of Node.js are you using?
14.17.6
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
not deployed, tested on localhost
Describe the Bug
Whenever I try to use the module jsonwebtoken
inside a middleware, I got the following warnings on the console:
warn - ./node_modules/events/events.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/function-bind/implementation.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/function-bind/index.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/get-intrinsic/index.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/has/src/index.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/is-generator-function/index.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/lodash.isplainobject/index.js
`eval` not allowed in Middleware pages/foo/_middleware
./node_modules/readable-stream/lib/_stream_writable.js
`eval` not allowed in Middleware pages/foo/_middleware
Expected Behavior
The app still works just fine, but I believe this warning shouldn’t happen, considering that this module can be used on both front and back end side of my app.
Btw, I haven’t done much with middlewares, so I was only able to reproduce this issue only with this specific npm module. If I notice that importing any other module logs the same warning I’ll update the issue here.
To Reproduce
you can try it on this repository: https://github.com/mauriciosoares/middlwares-and-rewrites/tree/eval-not-allowed-in-middleware, using the branch eval-not-allowed-in-middleware
.
To reproduce it you just need to access the url http://localhost:3000/foo/bar
, and the warning should appear on the console.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:16 (4 by maintainers)
Top GitHub Comments
Running Javascript code at the edge is very sensitive and providers are very conservative and ban certain APIs. Among many reasons there is speed or being able to optimize code by statically analyzing it.
The explanation from @gustavo-dev was great and indeed, you can use Next.js middleware with
eval
when runningnext start
. On the other hand, if you deploy middleware for example to Vercel,eval
will not in production.In order to help people don’t get into this situation, we are showing a warning in Next.js if we detect you are dynamically evaluating code. This warning will show up when the code is parsed. Note that currently in development there is no tree-shaking, so (at the time of writing this comment) it is possible that you get a warning about using eval in code that is required by the middleware but that will not make it to production since it may be removed on build thanks to tree-shaking.
I think you’re mixing the concepts a little bit.
Edge Functions are similar to CDN’s (but different).
A CDN (Content Delivery Network) is used to serve static content in a fast way. Therefore they need to be located close to the client. The downside with this approach is the inability to deliver custom content to different clients based on their location, for example.
Another method that could solve this would be processing each request on the server (SSR). However, the drawback now is speed because the server handling these requests is not necessarily close to your end-user.
That’s where Edge Functions come in! They work just like a CDN but they allow you to run server-side logic on the Edge, close to your client, which enables us to achieve both speed and dynamism.
A middleware, on the other hand, is a function that runs before a request is completed. If you’ve used ExpressJs before, you should be familiar with this concept. They can modify the request/response objects and are useful for authentication, among other things.
Hence, Middlewares are not necessarily Edge Functions and Edge Functions are not necessarily Middlewares. However, when deploying to Vercel, Middleware is the only code that runs in Edge Functions.
Here are a few links regarding these concepts if you want: