Next.js 13.4.2+Axios 1.4.0开发模式下follow-redirects依赖debug模块缺失问题
问题:Next.js 13.4.2 + Axios 1.4.0 开发模式下出现 'debug' 模块未找到警告
项目使用Next.js 13.4.2和Axios 1.4.0,开发模式下导入axios并请求数据时,控制台抛出警告:
Module not found: Can't resolve 'debug' in '\node_modules\follow-redirects'
错误提示建议用./debug替代debug,但不想修改node_modules源码。模块导入路径追踪如下:
./node_modules/follow-redirects/debug.js ./node_modules/follow-redirects/index.js ./node_modules/axios/lib/adapters/http.js ./node_modules/axios/lib/adapters/adapters.js ./node_modules/axios/lib/core/dispatchRequest.js ./node_modules/axios/lib/core/Axios.js ./node_modules/axios/lib/axios.js ./node_modules/axios/index.js ./src/components/login/LoginForm.tsx
需要无需修改源码的解决方案,比如利用resolve的preferRelative选项,或其他替代方案。
解决方案
方案1:通过Next.js配置启用preferRelative解析选项
在项目根目录的next.config.js中添加webpack解析配置,让follow-redirects模块优先使用相对路径导入:
/** @type {import('next').NextConfig} */ const nextConfig = { webpack: (config) => { config.resolve.preferRelative = true; return config; }, }; module.exports = nextConfig;
修改后重启开发服务器,webpack会优先解析相对路径的./debug而非尝试查找全局的debug包。
方案2:手动安装debug依赖包
直接安装缺失的debug包,让webpack能找到对应的模块:
npm install debug --save-dev # 或使用yarn yarn add debug --dev
方案3:调整依赖版本
- 升级Axios到最新稳定版(如v1.6+),新版本可能修复了与
follow-redirects的兼容问题 - 锁定
follow-redirects到导入逻辑更稳定的版本(如v1.15.2):
npm install follow-redirects@1.15.2 --save
内容的提问来源于stack exchange,提问作者hantoren
相关产品推荐
相关产品推荐

