You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展Manifest V3中导入Sentry模块报错TypeError求助

Chrome扩展Manifest V3本地集成Sentry解决Sentry.init is not a function报错

你遇到的问题根源在于直接下载的Sentry CDN文件(bundle.js/bundle.es5.js)是UMD格式,而非ES模块,在Manifest V3的模块类型Service Worker中导入后,无法直接通过Sentry.init调用方法。以下是两种可行的解决思路:

方法一:使用Sentry ES模块版本

  1. 将本地libs/sentry.js替换为对应版本的Sentry ES模块构建文件(如7.14.2版本的ES模块包)
  2. 保持原background.js代码不变即可正常调用:
import * as Sentry from './libs/sentry.js';

chrome.runtime.onStartup.addListener(() => {
  Sentry.init({
    dsn: "https://abcd@1234.ingest.sentry.io/666",
  });

  Sentry.captureMessage("test");
});

方法二:适配UMD格式的导入逻辑

如果继续使用已下载的UMD版本文件,需调整代码获取正确的Sentry实例:

import * as Sentry from './libs/sentry.js';

// UMD模块在ES模块环境中会将核心实例挂载在default属性上
const SentryClient = Sentry.default || Sentry;

chrome.runtime.onStartup.addListener(() => {
  SentryClient.init({
    dsn: "https://abcd@1234.ingest.sentry.io/666",
  });

  SentryClient.captureMessage("test");
});

验证要点

  • 确认manifest.json中background的type已设置为module,且minimum_chrome_version不低于92(Chrome 92及以上版本支持Service Worker ES模块)
  • 本地Sentry文件路径无拼写错误,确保能被正确加载

内容的提问来源于stack exchange,提问作者Vitalii Vasylenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:41:02