应用无法要求axios包

gz5pxeao  于 9个月前  发布在  iOS
关注(0)|答案(6)|浏览(173)

描述bug

  1. TypeError: Cannot read properties of undefined (reading 'charAt')
  2. at resolveURL (/Users/dennymueller/IdeaProjects/forest-school-saxony/node_modules/axios/dist/node/axios.cjs:3241:44)
  3. at standardBrowserEnv (/Users/dennymueller/IdeaProjects/forest-school-saxony/node_modules/axios/dist/node/axios.cjs:3247:17)
  4. at Object.<anonymous> (/Users/dennymueller/IdeaProjects/forest-school-saxony/node_modules/axios/dist/node/axios.cjs:3260:5)
  5. at Module._compile (node:internal/modules/cjs/loader:1254:14)
  6. at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
  7. at Module.load (node:internal/modules/cjs/loader:1117:32)
  8. at Module._load (node:internal/modules/cjs/loader:958:12)
  9. at Module.require (node:internal/modules/cjs/loader:1141:19)
  10. at require (node:internal/modules/cjs/helpers:110:18)
  11. at Object.<anonymous> (/Users/dennymueller/IdeaProjects/forest-school-saxony/node_modules/@nestjs/axios/dist/http.module.js:16:33)

上述错误导致我的应用程序在启动时失败。
这个错误明确发生在使用最新版本1.6.1,而不是1.6.0的情况下。
而且只有在我使用应用程序的生产构建,而不是开发模式时才会出现这个问题。
我正在使用webpack、nestjs、angular以及angular-universal和domino作为ssr的模拟。

重现问题

查看项目:
https://github.com/denny99/axios-ticket.git
运行:

  1. npm ci
  2. npm run build
  3. npm start

出现错误消息,表示axios包未安装。
这个陈述并不完全正确。它的原因是require axios失败了。
为了方便调试,我在backend/main.ts的第12行添加了一个require('axios')
现在它可以打印完整的堆栈跟踪。
将axios包更改为1.6.0

  1. npm i
  2. npm run build
  3. npm start

然后它再次启动,没有错误。

代码片段

系统在nest.js中要求这个包时崩溃。
在这段代码片段中失败:

  1. // nest.js original code
  2. const axios_1 = __importDefault(require("axios"));
  3. // code in axios.cjs
  4. return {
  5. href: urlParsingNode.href,
  6. protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
  7. host: urlParsingNode.host,
  8. search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
  9. hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
  10. hostname: urlParsingNode.hostname,
  11. port: urlParsingNode.port,
  12. pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
  13. urlParsingNode.pathname :
  14. '/' + urlParsingNode.pathname
  15. };

预期行为

没有崩溃,就像在1.6.0中一样。

Axios版本

1.6.1

适配器版本

  • 无响应*

浏览器

  • 无响应*

浏览器版本

  • 无响应*

Node.js版本

v18.16.0

OS

  • 无响应*

其他库版本

  1. NestJS ~10.0.0
  2. Angular ~16.2.0
  3. Domino ^2.1.6

其他上下文/截图

  • 无响应*
qhhrdooz

qhhrdooz1#

这个问题确实与多米诺骨牌SSR填充有关。
更具体地说:
global['document'] = win.document;
取消注解这一行可以让应用程序再次启动。
因此,“浏览器检查”必须与axios 1.6.0相比在1.6.1中发生了变化。

sycxhyv7

sycxhyv72#

这个可以改为空值检查吗:路径名:(urlParsingNode.pathname?.charAt(0) === '/')

fjaof16o

fjaof16o3#

关于这个问题的更新?
我正在遇到相同的问题
使用的是
Angular 17
@angular/ssr
domino-ext

mf98qq94

mf98qq944#

我通过将vitest配置环境从happy-dom更改为jsdom来解决这个问题,现在它可以正常工作了。

o2rvlv0m

o2rvlv0m5#

关于上述问题的任何更新?在Angular SSR中也遇到了相同的问题,尝试了不同的方法,但仍然没有成功。

t2a7ltrp

t2a7ltrp6#

我最终通过将vitest配置环境从happy-dom更改为jsdom来解决这个问题,它可以正常工作。
@jgreer9514 你能分享一下你的happy-dom配置吗?

相关问题