EvalError
In JavaScript, EvalError is the native error type that the engine throws when the host forbids building code from strings, as with eval() or new Function() under a strict security policy. It extends Error, so it carries a message, a name, and a stack trace. Say a program runs on a server started with Node.js’s --disallow-code-generation-from-strings flag:
calc.mjs
console.log(eval("2 + 2"));
The flag turns the eval() call into an error:
$ node --disallow-code-generation-from-strings calc.mjs
EvalError: Code generation from strings disallowed for this context
EvalError is a legacy type. The ECMAScript specification no longer throws it anywhere and keeps it only for compatibility with older editions, so most code never sees one.
When JavaScript Throws EvalError
The engine doesn’t throw EvalError for a bug in the code itself. It throws one at run time only when the host refuses to compile a string into code:
- Content Security Policy: In browsers, a page whose
script-srcdirective lacks'unsafe-eval'blockseval(),new Function(), and the other function constructors. The CSP specification requires anEvalErrorhere. - Node.js flag: The
--disallow-code-generation-from-stringsflag blocks the same calls in a program, thoughnode:vmcontexts keep their own setting. node:vmcontexts: A context created withcodeGeneration: { strings: false }blocks them for code that runs inside it.
Code rarely throws EvalError on purpose. Its constructor takes the same arguments as Error: an optional message and an options object with a cause.
Catching EvalError
Say a tool runs short scripts inside a node:vm context that forbids code generation, and reports the scripts that try it instead of crashing:
run-script.mjs
import vm from "node:vm";
const context = vm.createContext({}, { codeGeneration: { strings: false } });
function runScript(source) {
try {
return vm.runInContext(source, context);
} catch (error) {
if (error.name !== "EvalError") throw error;
return `Blocked: ${source}`;
}
}
console.log(runScript("[3, 1, 2].toSorted()"));
console.log(runScript('eval("2 + 2")'));
console.log(runScript('new Function("return 42")()'));
Only the first script runs:
$ node run-script.mjs
[ 1, 2, 3 ]
Blocked: eval("2 + 2")
Blocked: new Function("return 42")()
The check uses error.name because the error comes from the context’s own EvalError, so error instanceof EvalError is false here. The instanceof operator fails for errors from another realm, such as an iframe or a node:vm context, while error.name and Error.isError() still work.
Common Messages
Node.js and Chrome print these messages for EvalError. The Node.js message comes from V8, and the Chrome message comes from Blink, Chrome’s rendering engine:
| Message (V8) | Usual Cause | Fix |
|---|---|---|
Code generation from strings disallowed for this context |
Node.js: eval() or new Function() under --disallow-code-generation-from-strings or in a node:vm context that forbids it |
Replace the string-built code with a regular function, or drop the restriction |
Evaluating a string as JavaScript violates the following Content Security Policy directive because 'unsafe-eval' is not an allowed source of script: X |
Chrome: eval(), new Function(), or a library that compiles code from strings, on a page whose CSP lacks 'unsafe-eval' |
Remove the string compilation, or switch to a build of the library that doesn’t need it |
Firefox and Safari word the Content Security Policy error differently:
| V8 | Firefox (SpiderMonkey) | Safari (JavaScriptCore) |
|---|---|---|
Evaluating a string as JavaScript violates the following Content Security Policy directive because 'unsafe-eval' is not an allowed source of script: X |
call to eval() blocked by CSP or call to Function() blocked by CSP |
Refused to evaluate a string as JavaScript because 'unsafe-eval' or 'trusted-types-eval' is not an allowed source of script in the following Content Security Policy directive: "X". |
Before Chrome 142, Chrome printed Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "X". The fix for every row is code that doesn’t compile strings at run time.
Related Resources
Course
JavaScript Errors and Debugging for Pythonistas
Port a food bank's donation import to JavaScript, and learn to catch, throw, and tell apart errors, check for the bad data JavaScript doesn't throw on, and find what went wrong.
For additional information on related topics, take a look at the following resources:
- JavaScript Functions for Pythonistas (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026