Why is chrome.browserAction.onClicked undefined?

Classic ManifestV2

For those who already have added something like

"background": {
    "scripts": ["background.js"]
}

and still gets Cannot read property 'onClicked' of undefined - just add

"browser_action": {}

into your manifest.json

ManifestV3

manifest.json: use action not browser_action, see also the migration guide.

"background": {"service_worker": "background.js"}
"action": {}

background.js: use chrome.action not chrome.browserAction.


It seems like the code is in your twterland.js file, which is your content script. browserAction can only be used in extension pages, so you can not use it in content scripts.

Document: https://developer.chrome.com/extensions/content_scripts

However, content scripts have some limitations. They cannot:
- Use chrome.* APIs (except for parts of chrome.extension)
- Use variables or functions defined by their extension's pages
- Use variables or functions defined by web pages or by other content scripts

Put it on the background page instead.


If you do not have a "browser_action" property defined in your manifest.json then this error may occur. @Kirill's answer works but you also have to add a blank icon.png file else chrome will throw an error that it cannot find such a file.

Adding this to the manifest.json file should suppress this is error:

"browser_action": {}

Be sure to read the documentation for further reference on how to use the "browser_action" setting.


I was also getting this, adding

"persistent": true 

to my background declaration in manifest.json solved it.