打包错误Uncaught Error: Cannot find module 'axios/lib/adapters/http'

This issue has been tracked since 2021-05-14.

写了一个音乐应用使用了你spider文件的音乐接口,调试时可以请求到数据,app完美运行。
但打包时出现找不到模块

Uncaught Error: Cannot find module 'axios/lib/adapters/http'

经过我的测试,因该是

request.defaults.adapter = global.require('axios/lib/adapters/http')

这里出现问题,global.require所引入的依赖打包时被忽略,导致打包后缺少这个依赖。
不使用global.require,使用import又请求不到数据,请问这个有什么办法解决吗?

fengT-T wrote this answer on 2021-05-17

import httpAdapter from 'axios/lib/adapters/http'
request.defaults.adapter = httpAdapter
应该是可以的,

CGPer wrote this answer on 2021-05-17

我试过了你写的,请求不到数据。因为使用import时webpack把httpAdapter自动改为xhrAdapter导致请求数据失败。我想这也是为什么作者非得写成global.require而不是require的原因,还在那里diss了一下webpack。

fengT-T wrote this answer on 2021-05-17

哈哈哈,那就不清楚了,挺久了,

CGPer wrote this answer on 2021-05-23

试了很多方案,终于找到解决办法了。
首先在vue.config.js里添加webpack配置:
configureWebpack: { target:'electron-renderer' }
然后将node_modules/axios/lib/adapters里的http.js内容覆盖掉xhr.js里的内容。
最后将global.require改为require。这样就既可以请求到数据,又可以打包了。

doseeing wrote this answer on 2021-12-23

还有一种办法是修改node_modules目录下axios的package.json文件

  "browser": {
    "./lib/adapters/http.js": "./lib/adapters/xhr.js"
  },

修改为

  "browser": {
    "./lib/adapters/http.js": "./lib/adapters/http.js"
  },

或者直接删除。

More Details About Repo
Owner Name xtuJSer
Repo Name CoCoMusic
Full Name xtuJSer/CoCoMusic
Language JavaScript
Created Date 2017-07-20
Updated Date 2022-11-24
Star Count 929
Watcher Count 36
Fork Count 121
Issue Count 44

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date