Skip to content

lodash eval/new Function() usage causing Next.js Middleware to fail #5394

@blakewilson

Description

@blakewilson

Next.js middleware runs on a strict runtime, which does not allow eval or new Function(evalString):

The following JavaScript language features are disabled, and will not work:
eval: Evaluates JavaScript code represented as a string
new Function(evalString): Creates a new function with the code provided as an argument

Can Lodash still run in this environment? For example trim imports _root.js which uses this type of logic for a global object reference:

/** Used as a reference to the global object. */
var root = freeGlobal || freeSelf || Function('return this')();

This results in the following Next.js error during build:

Failed to compile.

../../../node_modules/lodash/_root.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware pages/_middleware

Import trace for requested module:
../../../node_modules/lodash/_Symbol.js
../../../node_modules/lodash/_baseToString.js
../../../node_modules/lodash/trim.js
./src/pages/_middleware.tsx

In regards to issue #3328, it seems like this behavior is avoidable. Is this still the case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue bankruptcyClosing the issue/PR to start fresh

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions