Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Electron app blank with 'Refused to evaluate a string as JavaScript'

ErrorUncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive

Short answer

The page has a Content-Security-Policy without 'unsafe-eval', and some code calls eval or new Function. In a React dev build the usual source is webpack's eval source maps. Change devtool to a non-eval value, or remove the eval call. Allow 'unsafe-eval' only as a last resort.

  • Applies to Electron, webpack 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which policy the window actually runs with: a meta tag in the HTML, or a header set in the main process.

    grep -rn "Content-Security-Policy" public/ src/ main.js electron/ 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The error appears with the dev server (webpack development mode)webpack development build uses eval source mapsFix
    2. 2The stack trace points to a file that is not a webpack module wrapperYour own or a library's code calls eval, new Function or a string timerFix
    3. 3You edited one policy but the error staysThe policy is set in a different place than the one you editedFix

What does the error look like?

  • Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive

Cause 1 of 3

webpack development build uses eval source maps

1Check

In development mode webpack defaults devtool to eval, and every eval- value runs each module through eval().

grep -rn "devtool" webpack.config.* craco.config.* 2>/dev/null

2Fix

Set a value without the eval prefix, for example source-map.

// webpack.config.js
module.exports = {
  devtool: "source-map",
};

3Verify

Restart the dev server. The window loads and the console shows no EvalError.

npm start

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

Your own or a library's code calls eval, new Function or a string timer

1Check

Without 'unsafe-eval' in script-src, the browser blocks eval(), Function() and string arguments to setTimeout and setInterval.

grep -rnE "eval\(|new Function\(|setTimeout\(['\"]" src/

2Fix

Replace the string with a real function. For a library, upgrade it or pick one that does not evaluate strings. Only if neither is possible add 'unsafe-eval' to script-src for that window, knowing it weakens the policy.

// before
setTimeout("save()", 500);
// after
setTimeout(() => save(), 500);

3Verify

Reload the window with developer tools open; the error does not return.

npm start

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The policy is set in a different place than the one you edited

1Check

Electron documents two ways to set a policy: an HTTP header, which it prefers, and a meta tag for file:// pages. Make sure you edit the one your app really uses.

grep -rn "onHeadersReceived" main.js electron/ src/ 2>/dev/null

2Fix

Keep one policy. If you must change it, change the place that is really in use, and keep script-src restrictive, for example script-src 'self'.

// main process, after app.whenReady()
const { session } = require('electron');
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
  callback({
    responseHeaders: {
      ...details.responseHeaders,
      'Content-Security-Policy': ["script-src 'self'"],
    },
  });
});

3Verify

Restart the app, not only reload it, because the main process code changed.

npm start

Confirmed by electronjs.org

Did this fix it?

How do I stop it coming back?

  • Use a non-eval devtool in the Electron renderer build from the start.
  • Keep script-src at 'self' and do not add 'unsafe-eval' to silence a warning.
  • Check new dependencies for eval use before adding them to the renderer.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Jest fails when a component imports an .svg file

A syntax error is reported for the .svg file, because Jest tries to parse it as JavaScript

Jest runs every imported file as JavaScript, and an .svg file is XML, not JavaScript.

3 causes, each with a fix

Web apps

Jest fails to parse a React Native or Expo dependency

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax

Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.

3 causes, each with a fix

Web apps

TypeScript says a JavaScript module implicitly has an any type

Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.

TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.