whatsapp-web.js 扫码登录后出现Cannot read properties of undefined (reading 'default')错误求助
我之前也碰到过类似的无头浏览器DOM解析相关问题,结合社区解决方案和自己的踩坑经验,给你几个可行的排查和解决方向:
问题复现
按照入门指南编写了以下代码:
const { Client } = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); const client = new Client(); client.on('ready', () => { console.log('Client is ready!'); }); client.on('qr', qr => { qrcode.generate(qr, {small: true}); }); client.initialize();
执行node main.js后终端能正常生成二维码,但扫码登录时触发错误:
0|main | Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'default')
0|main | at puppeteer_evaluation_script:5:95
0|main | at ExecutionContext._evaluateInternal (/home/ubuntu/haro-node/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
0|main | at process....
解决方案
版本兼容性排查
whatsapp-web.js对puppeteer的版本有严格依赖,版本不匹配极易触发这类DOM读取错误。建议查看你安装的whatsapp-web.js版本对应的puppeteer要求(可查看项目package.json里的peerDependencies),然后重新安装适配的puppeteer版本:npm uninstall puppeteer npm install puppeteer@<对应版本号>清除旧会话缓存
whatsapp-web.js会默认在本地生成.wwebjs_auth文件夹存储登录会话缓存,旧缓存可能和当前库版本不兼容。删除这个文件夹后,重新启动脚本扫码登录,大概率能解决缓存冲突问题。开启非无头模式调试
无头模式下puppeteer的DOM解析可能存在隐性问题,你可以修改Client配置,开启可视化浏览器窗口,查看登录时的页面具体错误:const client = new Client({ puppeteer: { headless: false } });启动后会弹出Chrome窗口,打开开发者工具的控制台,能看到更详细的页面报错信息,方便精准定位问题。
更新到最新稳定版
旧版本的whatsapp-web.js可能存在已知的登录相关bug,尝试更新到最新稳定版:npm update whatsapp-web.js
备注:内容来源于stack exchange,提问作者user15254140

