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

React导入react-export-table-to-excel报模块未找到错误如何解决

报错信息
Module not found: Error: Can't resolve 'react-export-table-to-excel'
问题背景

项目需要实现React表格导出Excel功能,安装react-export-table-to-excel依赖后,使用以下语句引入组件时触发上述报错:

import { DownloadTableExcel } from 'react-export-table-to-excel';

尝试全局安装该依赖后问题没有解决。

相关文件截图
  • node_modules目录下依赖文件截图1:
    依赖目录截图1
  • node_modules目录下依赖文件截图2:
    依赖目录截图2
项目package.json配置
{
  "name": "react app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@coreui/coreui": "^4.1.6",
    "@coreui/react": "^4.2.3",
    "@date-io/date-fns": "^2.14.0",
    "@date-io/dayjs": "^2.14.0",
    "@date-io/luxon": "^2.14.0",
    "@date-io/moment": "^2.14.0",
    "@emotion/react": "^11.9.3",
    "@emotion/styled": "^11.9.3",
    "@mui/x-data-grid": "^5.12.3",
    "@mui/x-date-pickers": "^5.0.0-alpha.7",
    "@mui/x-date-pickers-pro": "^5.0.0-alpha.7",
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "autoprefixer": "10.4.5",
    "axios": "^0.27.2",
    "bootstrap": "^5.1.3",
    "date-fns": "^2.28.0",
    "react": "^18.2.0",
    "react-bootstrap": "^2.4.0",
    "react-bootstrap-sweetalert": "^5.2.0",
    "react-date-range": "^1.4.0",
    "react-dom": "^18.2.0",
    "react-export-table-to-excel": "^1.0.3",
    "react-icons": "^4.4.0",
    "react-redux": "^8.0.2",
    "react-scripts": "5.0.1",
    "rsuite": "^5.15.1",
    "web-vitals": "^2.1.4",
    "xlsx": "^0.18.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
问题原因
  • 全局安装依赖无效:React项目构建时只会检索项目根目录下node_modules文件夹内的本地依赖,全局安装的包不会被项目识别。
  • 依赖安装损坏:安装过程中网络波动、缓存异常会导致包文件写入不完整,构建工具无法找到模块入口。
  • 版本Bug:当前安装的react-export-table-to-excel@1.0.3版本存在入口配置错误,包内实际文件路径和package.json声明的导出路径不匹配,直接导致模块解析失败,从提供的node_modules截图可以验证该问题。
  • 构建缓存未更新:react-scripts内置的webpack缓存会存储旧的依赖解析结果,新安装依赖后如果不重启服务、清除缓存,会持续报解析错误。
解决步骤
  1. 清除缓存并重装本地依赖,在项目根目录依次执行以下命令:
# 删除现有依赖文件和锁文件
rm -rf node_modules package-lock.json
# 清除npm安装缓存
npm cache clean --force
# 重新安装所有项目依赖
npm install
  1. 如果重装后仍报错,直接替换存在Bug的1.0.3版本,执行以下命令安装稳定版:
# 卸载现有问题版本
npm uninstall react-export-table-to-excel
# 安装无入口配置问题的1.0.2版本
npm install react-export-table-to-excel@1.0.2
  1. 完全终止当前运行的开发服务,重新执行npm start启动项目,不要依赖热更新加载新安装的依赖。

内容的提问来源于stack exchange,提问作者lewis machilika

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 09:45:33