2025年 GitHub 与 Stack Overflow 中 20 大经典前端 Bug 总结

🧠 2025年 GitHub 与 Stack Overflow 中 20 大经典前端 Bug 总结

涵盖 Vue、React、JavaScript、TypeScript、CSS 等主流技术栈,附带详细注释与排查时间估算(适合面试 & 实战)


📌 背景说明

在 2024-2025 年的前端开发社区中,GitHub Issues 和 Stack Overflow 上出现了大量关于现代框架(Vue 3、React 18+、Next.js、Svelte)和语言特性(TypeScript、ES Modules、Web Components)的常见 bug。以下是老曹整理的 20 个最经典且高频出现的前端 Bug,并按技术栈分类,每个都包含:

  • ✅ Bug 描述
  • 🔍 报错信息或表现
  • ⚙️ 常见原因
  • 💡 解决方案
  • 🕒 排查时间预估(适用于初学者 / 高级开发者)
  • 🧪 示例代码 + 注释
  • 📌 来源链接(GitHub Issue / Stack Overflow)

🎯 分类目录

类别内容
🟨 JavaScript 基础类异步问题、类型错误、内存泄漏等
🟩 TypeScript 类型类类型推断失败、泛型不匹配、类型守卫未生效
🟥 CSS/样式类响应式失效、flex 布局异常、z-index 不生效
🟦 Vue 组件类生命周期问题、响应式更新失败、v-model 同步异常
🟪 React 组件类useEffect 依赖项遗漏、组件未卸载、ref 使用不当
🟫 构建工具类Webpack 打包错误、Vite 配置问题、Babel 编译失败
🟰 第三方库类Axios 请求失败、Element Plus/Vant 兼容性问题
🟱 浏览器兼容类Safari 移动端样式异常、IE11 Polyfill 缺失

📚 20 个经典前端 Bug 清单(含详细分析)


❗Bug #1:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

  • 平台:Webpack + Babel + CommonJS
  • 报错信息
    Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    
  • 常见场景
    • 在 ES Module 项目中混用 require()module.exports
    • 使用了不兼容的第三方模块(如旧版 UMD 模块)
  • 解决方案
    • 使用 import 替代 require()
    • 升级第三方库版本
    • 修改 Webpack 配置为 experiments.outputModule: true(ESM 输出)
  • 排查时间
    • 初级:1~2 小时
    • 高级:15~30 分钟
  • 来源GitHub Issue

❗Bug #2:TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.

  • 平台:React / Vue
  • 报错信息
    TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.
    
  • 常见场景
    • 从接口获取的数据为空,直接解构导致崩溃
    • props 默认值未设置,父组件未传参
  • 解决方案
    • 设置默认值:const { name = 'default' } = props;
    • 使用可选链:props?.name
    • 添加类型检查(PropTypes / TypeScript)
  • 排查时间
    • 初级:30 分钟 ~ 1 小时
    • 高级:10 分钟内定位
  • 来源Stack Overflow

❗Bug #3:Cannot find module 'vue' or its type declarations.

  • 平台:VSCode + TypeScript + Vue 3
  • 报错信息
    Cannot find module 'vue' or its type declarations.
    
  • 常见场景
    • Vue 未安装或未正确引入
    • tsconfig.json 未配置路径映射
  • 解决方案
    // tsconfig.json
    {
      "compilerOptions": {
        "types": ["vite/client", "vue"]
      }
    }
    
    • 安装 vue 类型:npm install @types/vue --save-dev
  • 排查时间
    • 初级:1~2 小时
    • 高级:10 分钟内解决
  • 来源GitHub Issue

❗Bug #4:Maximum update depth exceeded. React will block this component from rendering again...

  • 平台:React
  • 报错信息
    Maximum update depth exceeded. React will block this component from rendering again...
    
  • 常见场景
    • useEffect 中修改状态,但未设置依赖项
    • 循环调用 setState
  • 解决方案
    useEffect(() => {
      setData(fetchData());
    }, [dependency]); // 必须添加依赖项
    
  • 排查时间
    • 初级:1 小时以上
    • 高级:15 分钟内定位
  • 来源Stack Overflow

❗Bug #5:TypeError: Cannot set properties of undefined (setting 'token')

  • 平台:Vue / React + Vuex / Redux
  • 报错信息
    TypeError: Cannot set properties of undefined (setting 'token')
    
  • 常见场景
    • Vuex store 未正确挂载
    • 使用了未定义的对象属性(如 store.state.auth.token 但 auth 为 undefined)
  • 解决方案
    state: {
      auth: null,
    }
    
    • 初始化对象属性避免空引用
    • 使用可选链 state.auth?.token
  • 排查时间
    • 初级:30 分钟 ~ 1 小时
    • 高级:10 分钟内定位
  • 来源GitHub Issue

❗Bug #6:Component name “XXX” should always be multi-word

  • 平台:Vue 3 + ESLint
  • 报错信息
    Component name “Header” should always be multi-word
    
  • 常见场景
    • 单词命名组件违反 Vue 最佳实践
  • 解决方案
    export default defineComponent({
      name: 'MainHeader', // 改为多单词
    });
    
    • 关闭规则(不推荐):
    // .eslintrc.js
    rules: {
      'vue/multi-word-component-names': 0
    }
    
  • 排查时间
    • 初级:10 分钟
    • 高级:快速识别并修复
  • 来源GitHub Issue

❗Bug #7:Property 'value' does not exist on type '{}'

  • 平台:TypeScript + Vue 3 Composition API
  • 报错信息
    Property 'value' does not exist on type '{}'
    
  • 常见场景
    • 使用 reactive()ref() 未指定初始值
  • 解决方案
    const form = reactive<{ value?: string }>({});
    
    • 显式声明类型
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟内解决
  • 来源Stack Overflow

❗Bug #8:Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received

  • 平台:Chrome 扩展开发
  • 报错信息
    Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received
    
  • 常见场景
    • Chrome 扩展中异步返回数据未使用 sendResponse
  • 解决方案
    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
      setTimeout(() => {
        sendResponse({ data: 'success' });
      }, 1000);
      return true;
    });
    
  • 排查时间
    • 初级:2 小时
    • 高级:30 分钟
  • 来源GitHub Issue

❗Bug #9:The requested module 'xxx' does not provide an export named 'default'

  • 平台:Vite + ESM + React / Vue
  • 报错信息
    The requested module 'xxx' does not provide an export named 'default'
    
  • 常见场景
    • 使用了 CJS 模块却以 ESM 方式导入
    • 动态导入路径错误
  • 解决方案
    import MyComponent from './MyComponent.vue'; // ✅ 正确
    import { default as MyComponent } from './MyComponent.vue'; // ✅ 可用于 CJS 模块
    
  • 排查时间
    • 初级:1 小时
    • 高级:10 分钟
  • 来源GitHub Issue

❗Bug #10:TypeError: Cannot use ‘in’ operator on non-iterable instance

  • 平台:React / Vue + Babel
  • 报错信息
    TypeError: Cannot use 'in' operator on non-iterable instance
    
  • 常见场景
    • 对非数组/对象使用展开运算符
    • 使用了未初始化的 ref
  • 解决方案
    const arr = ref<number[]>([]);
    const list = [...arr.value]; // ✅ 使用 .value
    
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟内定位
  • 来源Stack Overflow

❗Bug #11:ReferenceError: regeneratorRuntime is not defined

  • 平台:Webpack + async/await
  • 报错信息
    ReferenceError: regeneratorRuntime is not defined
    
  • 常见场景
    • 未启用 Babel 的 regenerator-runtime 支持
  • 解决方案
    npm install --save-dev @babel/plugin-transform-runtime
    
    // babel.config.js
    plugins: ['@babel/plugin-transform-runtime']
    
  • 排查时间
    • 初级:1 小时
    • 高级:15 分钟
  • 来源GitHub Issue

❗Bug #12:Unexpected token 'export'

  • 平台:Node.js + ES Modules
  • 报错信息
    Unexpected token 'export'
    
  • 常见场景
    • 使用 import/export 但在 Node 环境中运行 .js 文件
  • 解决方案
    • 使用 .mjs 后缀
    • 设置 "type": "module" in package.json
    • 使用 node --experimental-specifier-resolution=node 运行
  • 排查时间
    • 初级:1~2 小时
    • 高级:15 分钟
  • 来源GitHub Issue

❗Bug #13:TypeError: Cannot read property 'map' of undefined

  • 平台:React / Vue
  • 报错信息
    TypeError: Cannot read property 'map' of undefined
    
  • 常见场景
    • 数据未加载完成就执行 map
    • 接口返回结构错误
  • 解决方案
    const list = data ? data.items : [];
    list.map(...) // ✅ 加保护
    
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟
  • 来源Stack Overflow

❗Bug #14:TypeError: Failed to fetch dynamically imported module

  • 平台:Vue 3 + Vite + 动态导入
  • 报错信息
    TypeError: Failed to fetch dynamically imported module
    
  • 常见场景
    • 动态导入路径错误
    • 路由懒加载失败
  • 解决方案
    () => import('../views/Dashboard.vue') // ✅ 路径必须相对正确
    
    • 检查打包输出目录结构
  • 排查时间
    • 初级:1 小时
    • 高级:15 分钟
  • 来源GitHub Issue

❗Bug #15:TypeError: Cannot convert undefined or null to object

  • 平台:React / Vue + Object.keys / for…in
  • 报错信息
    TypeError: Cannot convert undefined or null to object
    
  • 常见场景
    • null / undefined 使用 Object.keys
  • 解决方案
    const keys = Object.keys(obj || {}); // ✅ 做空值处理
    
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟
  • 来源Stack Overflow

❗Bug #16:TypeError: Cannot set property 'innerText' of null

  • 平台:原生 JS / DOM 操作
  • 报错信息
    TypeError: Cannot set property 'innerText' of null
    
  • 常见场景
    • DOM 元素未找到
    • 组件未渲染完就操作 DOM
  • 解决方案
    const el = document.getElementById('my-el');
    if (el) el.innerText = 'Hello';
    
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟
  • 来源GitHub Issue

❗Bug #17:TypeError: Cannot spread non-iterable instance

  • 平台:React / Vue + 展开语法
  • 报错信息
    TypeError: Cannot spread non-iterable instance
    
  • 常见场景
    • 对象不是数组但使用 ...
  • 解决方案
    const arr = Array.isArray(data) ? data : [];
    
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟
  • 来源Stack Overflow

❗Bug #18:TypeError: Cannot read property 'length' of undefined

  • 平台:React / Vue + 数组访问
  • 报错信息
    TypeError: Cannot read property 'length' of undefined
    
  • 常见场景
    • 数据未赋值前就访问 length
  • 解决方案
    const count = items ? items.length : 0;
    
  • 排查时间
    • 初级:30 分钟
    • 高级:5 分钟
  • 来源GitHub Issue

❗Bug #19:Uncaught (in promise) TypeError: Failed to execute 'put' on 'Cache': Request failed

  • 平台:PWA + Service Worker
  • 报错信息
    Failed to execute 'put' on 'Cache': Request failed
    
  • 常见场景
    • 请求失败后仍尝试缓存
  • 解决方案
    fetch(request).then(response => {
      if (response.ok) cache.put(request, response);
    });
    
  • 排查时间
    • 初级:1 小时
    • 高级:15 分钟
  • 来源GitHub Issue

❗Bug #20:RangeError: Maximum call stack size exceeded

  • 平台:React / Vue + 递归组件
  • 报错信息
    RangeError: Maximum call stack size exceeded
    
  • 常见场景
    • 组件自身嵌套调用
    • 无限循环(如 computed 返回自身)
  • 解决方案
    const computedValue = computed(() => {
      return computedValue.value + 1; // ❌ 死循环
    });
    
    • 避免自引用计算
  • 排查时间
    • 初级:1 小时
    • 高级:15 分钟
  • 来源Stack Overflow

📊 Bug 总览表格

序号报错信息技术栈排查难度时间预估
1Cannot assign to read only property ‘exports’Webpack1h
2Cannot destructure property ‘name’React/Vue30min
3Cannot find module ‘vue’TypeScript1h
4Maximum update depth exceededReact1h
5Cannot set properties of undefinedVuex/Redux30min
6Component name should be multi-wordVue10min
7Property ‘value’ does not exist on type ‘{}’TypeScript30min
8Listener returned true but no response sentChrome 扩展1h30min
9Does not provide an export named ‘default’Vite30min
10Cannot use ‘in’ operator on non-iterableReact/Vue30min
11RegeneratorRuntime is not definedBabel1h
12Unexpected token ‘export’Node.js1h
13Failed to read ‘length’ of undefinedReact/Vue30min
14Failed to fetch dynamically imported moduleVue/Vue Router30min
15Cannot set property ‘innerText’ of null原生 JS5min
16Cannot spread non-iterable instanceReact/Vue30min
17Failed to execute ‘put’ on ‘Cache’PWA1h
18Maximum call stack size exceededReact/Vue1h
19Cannot use ‘in’ operator on non-iterableReact/Vue30min
20Failed to load resource: net::ERR_BLOCKED_BY_CLIENT浏览器插件拦截5min

🛠️ 排查建议总结表

场景排查技巧
控制台报错查看完整堆栈,定位源头
网络请求失败查看 Network 面板,检查 status code
样式异常使用 Elements 面板审查 DOM 和 CSS
跨域问题检查 Access-Control-Allow-Origin
异步错误使用 try/catch / .catch()
类型错误使用 TypeScript 提前捕获
构建失败查看 webpack/vite 构建日志
内存泄漏使用 Performance 面板 Memory Profiler
状态管理使用 Devtools 查看 store 更新路径
第三方库问题查看 issue / 文档 / 版本是否兼容

📦 推荐调试工具清单

工具用途
Chrome DevTools主流调试工具
Vue DevtoolsVue 组件状态跟踪
React DevtoolsReact 组件树、props、state
Lighthouse页面性能评分
Fiddler / Charles网络请求抓包
Sentry线上错误收集
ESLint / Prettier语法规范检查
Jest / Cypress自动化测试
Git Bisect定位哪个 commit 引入 bug

📚 推荐学习资源

类型推荐资料
Vue《Vue 3 设计与实现》
React《React Hooks 详解》
TypeScript《TypeScript 深度解析》
构建工具《Webpack 从入门到进阶》
线上监控《前端监控实战》
开发规范Airbnb Style Guide
项目架构《大型前端架构设计》

🌟 持续更新中:如你有实际遇到的 Bug,也可以评论区评论,老曹来帮你诊断并加入此清单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值