Maaz Zindani

Loading the index…

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

A Chrome extension built with webpack is blocked for using eval

Errorunsafe-eval

Short 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.

  • Applies to webpack 5, Chrome 140 (Manifest V3), Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/*.js
  2. 2Match what you see, then jump to its fix

    1. 1The error appears with a development build, and the bundle is full of eval( callswebpack development mode uses devtool evalFix
    2. 2The error is still there with devtool set to source-map, and points at a library fileA library in the bundle evaluates stringsFix
    3. 3Insecure CSP value "'unsafe-eval'" in directive 'script-src'.You added unsafe-eval to the manifest policyFix

What does the error look like?

  • unsafe-eval
  • Insecure 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.js

2Fix

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/*.js

Confirmed by webpack.js.org

Did this fix it?

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/*.js

2Fix

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 extension

Confirmed by developer.chrome.com

Did this fix it?

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.json

2Fix

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://extensions

Confirmed by developer.chrome.com

Did this fix it?

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 fix

Next step

Want this handled for good?

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