Next.js站点部署Hostinger时出现Sentry相关TypeError报错
Next.js集成Sentry部署Hostinger后站点崩溃问题
本地运行时Sentry功能正常,站点无异常,但部署到Hostinger后直接崩溃,控制台报错如下:
512-fd82f752a414c660.js:525 TypeError: Cannot read properties of undefined (reading 'sentry') at 164.1b37e5fe1265aae3.js:1:21471 at Array.reduce (<anonymous>) at q (164.1b37e5fe1265aae3.js:1:21456) at n (164.1b37e5fe1265aae3.js:1:34922) at 164.1b37e5fe1265aae3.js:17:64552 at oO (164.1b37e5fe1265aae3.js:17:67668) at oA (164.1b37e5fe1265aae3.js:17:63598) at 164.1b37e5fe1265aae3.js:17:63405 at oP (164.1b37e5fe1265aae3.js:17:63412) at ov (164.1b37e5fe1265aae3.js:17:60278) at og (164.1b37e5fe1265aae3.js:17:58850) at S (164.1b37e5fe1265aae3.js:25:1364) at MessagePort.T (164.1b37e5fe1265aae3.js:25:1894)
我自己的代码里没有主动调用过任何对象的.sentry属性,当前的sentry.client.config.ts配置如下:
Sentry.init({ dsn: "https://c14ecceb9e4b2cb37fcf625c50cb91db@o4508458061332480.ingest.us.sentry.io/4508458063233024", integrations: [ Sentry.replayIntegration(), Sentry.feedbackIntegration({ // Additional SDK configuration goes in here, for example: colorScheme: "dark", }), ], production, or use tracesSampler for greater control. tracesSampleRate: 1, while replaysSessionSampleRate: 1.0, replaysOnErrorSampleRate: 1.0, debug: false, });
控制台报错截图显示上述TypeError信息,报错位置开发者工具截图指向编译后的JS文件中尝试读取sentry属性的代码段。
可能的解决方法:
- 修复配置文件语法错误:你的配置文件里有两行无效的注释(
production, or use tracesSampler for greater control.和while),这会导致编译异常。修正后的配置如下:Sentry.init({ dsn: "https://c14ecceb9e4b2cb37fcf625c50cb91db@o4508458061332480.ingest.us.sentry.io/4508458063233024", integrations: [ Sentry.replayIntegration(), Sentry.feedbackIntegration({ colorScheme: "dark", }), ], // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring tracesSampleRate: 1, // Capture 100% of sessions for replays, adjust as needed replaysSessionSampleRate: 1.0, replaysOnErrorSampleRate: 1.0, debug: false, }); - 检查环境变量配置:确认Hostinger部署面板中是否正确设置了Sentry相关环境变量(比如
NEXT_PUBLIC_SENTRY_DSN),变量名要和本地.env文件完全一致,避免部署时DSN未正确注入。 - 清理构建缓存:本地先执行
npm run build重新生成构建文件,或者在Hostinger的部署设置中清空缓存后重新部署,避免旧的缓存文件导致异常。 - 核对SDK与Next.js版本兼容性:检查
@sentry/nextjs等依赖的版本是否和你使用的Next.js版本兼容,建议升级到最新稳定版后重新构建部署。 - 确认Hostinger构建命令:确保Hostinger使用的构建命令是
npm run build,避免因构建不完整导致Sentry SDK初始化失败。
内容的提问来源于stack exchange,提问作者Toasty
相关产品推荐
相关产品推荐

