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.

`eval` not allowed in Middleware pages/foo/_middleware

See original GitHub issue

What 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

image

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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
javivelascocommented, Nov 2, 2021

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 running next 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.

5reactions
tavindevcommented, Oct 31, 2021

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:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js fails to build due to Dynamic Code Evaluation not ...
i figured it out, as juliomalves suggested that the apollo client auth link was the problem because the nextjs middleware is incapable of ......
Read more >
Edge Middleware Limitations – Vercel Docs
Learn about the limitations of using Edge Middleware with Vercel. ... Dynamic code execution (such as eval ) is not allowed (see the...
Read more >
Some problems with nextjs12 middleware and upgrade from ...
node_modules/react/cjs/react.development.js `eval` not allowed in Middleware pages/_middleware error - SyntaxError: Unexpected token ...
Read more >
Edge Runtime - Next.js
Calling require directly is not allowed. Use ES Modules instead. The following JavaScript language features are disabled, and will not work: eval :...
Read more >
JavaScript eval() Method - W3Schools
Execute JavaScript code with eval():. let x = 10; let y = 20; ... Do NOT use eval() ... ES1 (JavaScript 1997) is...
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