如何修复whatsapp-web.js中的TypeError: Cannot read properties of null (reading '1')
解决whatsapp-web.js第二次运行时的TypeError错误
问题现象
直接运行代码第二次会抛出以下错误:
const version = indexHtml.match(/manifest-([\d.]+).json/)[1];
^
TypeError: Cannot read properties of null (reading '1')
at LocalWebCache.persist (/Users/abc/Desktop/haba/node_modules/whatsapp-web.js/src/webCache/LocalWebCache.js:29:69)
at /Users/abc/Desktop/haba/node_modules/whatsapp-web.js/src/Client.js:744:36
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
但使用npx nodemon index.js运行时无异常。
解决方法
- 清理默认缓存目录:找到项目根目录下的
.wwebjs_cache文件夹,删除后重新运行代码,让程序重新生成正确的缓存文件。 - 禁用缓存:初始化Client时关闭缓存功能,避免缓存持久化逻辑触发错误,示例代码:
const { Client } = require('whatsapp-web.js'); const client = new Client({ cacheEnabled: false });
- 指定自定义缓存目录:配置一个有读写权限的自定义缓存路径,确保缓存文件能正常读写,示例:
const { Client } = require('whatsapp-web.js'); const client = new Client({ cachePath: './my_whatsapp_cache' });
- 检查目录权限:确认项目所在目录对当前用户具备读写权限,防止缓存文件写入失败导致后续读取的indexHtml内容异常。
内容的提问来源于stack exchange,提问作者Godwin Tobi
相关产品推荐
相关产品推荐

