A Chrome extension built with webpack is blocked for using eval
unsafe-evalShort answer
Extension pages cannot evaluate strings as code, and Manifest V3 does not let you add unsafe-eval. Webpack in development mode defaults to devtool eval, which wraps every module in eval(). Set devtool to a non-eval value such as source-map or false, and replace any library that evaluates strings.
Fix it fast
1Find out which cause you have
Open the built bundle and check whether modules are wrapped in eval(). If they are, the devtool setting is the cause.
grep -c "eval(" dist/*.js2Match what you see, then jump to its fix
- 1
The error appears with a development build, and the bundle is full of eval( callswebpack development mode uses devtool evalFix - 2
The error is still there with devtool set to source-map, and points at a library fileA library in the bundle evaluates stringsFix - 3
Insecure CSP value "'unsafe-eval'" in directive 'script-src'.You added unsafe-eval to the manifest policyFix
- 1
What does the error look like?
unsafe-evalInsecure CSP value "'unsafe-eval'" in directive 'script-src'.
Cause 1 of 3
webpack development mode uses devtool eval
1Check
In development mode the default devtool is eval, which executes each module with eval() and a sourceURL comment.
grep -n "devtool\|mode" webpack.config.js2Fix
Choose a devtool value that does not start with eval. source-map writes a separate map file. Use false to turn source maps off.
// webpack.config.js
module.exports = {
mode: 'development',
devtool: 'source-map',
};3Verify
Rebuild and check that the bundle has no eval wrappers, then reload the extension.
npx webpack
grep -c "eval(" dist/*.jsConfirmed by webpack.js.org
Cause 2 of 3
A library in the bundle evaluates strings
1Check
By default an extension page cannot evaluate strings as executable code, whatever webpack does. Find which file calls eval or new Function.
grep -ln "new Function(\|eval(" dist/*.js2Fix
Replace or update the library to a build that does not evaluate strings. If you cannot, run that code in a sandboxed page, because the default sandbox policy allows unsafe-eval.
// manifest.json (sandboxed page, if you really need eval)
"sandbox": { "pages": ["sandbox.html"] }3Verify
Reload the extension and trigger the feature that used the library.
# open chrome://extensions and check the Errors button of the extensionConfirmed by developer.chrome.com
Cause 3 of 3
You added unsafe-eval to the manifest policy
1Check
The minimum policy for extension pages cannot be loosened. Adding unsafe-eval to content_security_policy.extension_pages causes an install error.
grep -n -A3 "content_security_policy" manifest.json2Fix
Remove unsafe-eval from extension_pages and fix the source of the eval, as in the two causes above.
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self';"
}3Verify
The extension loads without an install error.
# reload the extension on chrome://extensionsConfirmed by developer.chrome.com
How do I stop it coming back?
- Set devtool explicitly in the config instead of relying on the mode default.
- Build the extension with a production or source-map config in CI, and test that exact output.
- Before adding a library to an extension, check that it does not evaluate strings.
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
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix