Electron app blank with 'Refused to evaluate a string as JavaScript'
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 directiveShort 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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
The error appears with the dev server (webpack development mode)webpack development build uses eval source mapsFix - 2
The 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
You edited one policy but the error staysThe policy is set in a different place than the one you editedFix
- 1
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/null2Fix
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 startConfirmed by webpack.js.org
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 startConfirmed by developer.mozilla.org
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/null2Fix
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 startConfirmed by electronjs.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
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 fixWeb apps
A React component crashes while destructuring props or state
TypeError: Cannot destructure property 'b' of 'null' as it is null.
JavaScript throws this when the right side of a destructuring pattern is undefined or null.
3 causes, each with a fixWeb 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 fixWeb apps
Sentry: Non-Error promise rejection captured with value in a React app
Non-Error exception (or promise rejection) captured with keys: x, y, z
Sentry raises this event when something that is not an Error object is thrown or used to reject a promise, for example a plain object from an API.
3 causes, each with a fixWeb apps
fetch() throws TypeError: Failed to fetch in the browser
Uncaught (in promise) TypeError: Failed to fetch
Failed to fetch means the request never produced a response the browser would hand to your code.
4 causes, each with a fixWeb 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