使用aliexpress-product-scraper库遇Uncaught TypeError错误求助
解决 aliexpress-product-scraper 中 "Cannot read properties of undefined (reading 'bind')" 错误
问题重现
运行以下代码获取速卖通商品详情时:
const scrape = require('aliexpress-product-scraper'); const product = scrape('32958933105'); product.then(res => { console.log('The JSON: ', res); });
抛出如下错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'bind') at 1777.../common/Debug.js (bundle.alisavetools.js:183218:50) at o (bundle.alisavetools.js:1:265) at bundle.alisavetools.js:1:316 at 1782.../common/Puppeteer.js (bundle.alisavetools.js:184542:29) at o (bundle.alisavetools.js:1:265) at bundle.alisavetools.js:1:316 at Object.<anonymous> (bundle.alisavetools.js:183047:24) at Object.<anonymous> (bundle.alisavetools.js:183071:4) at 1775../node/Puppeteer.js (bundle.alisavetools.js:183071:17) at o (bundle.alisavetools.js:1:265)
解决方案
- 检查 Node.js 版本兼容性:该库依赖 Puppeteer,而 Puppeteer 要求 Node.js 14 及以上版本。运行
node -v确认版本,若过低则升级到 LTS 版本(如 18.x、20.x)。 - 重新安装依赖:删除本地依赖文件和锁文件后重新安装,同时清理缓存:
rm -rf node_modules package-lock.json npm cache clean --force npm install - 显式指定 Puppeteer 版本:错误指向 Puppeteer 模块,可能是库内置版本与环境不兼容。手动安装兼容版本:
版本号可根据实际情况调整,优先选择库文档推荐的版本。npm install puppeteer@20.0.0 - 调整代码调用逻辑:改用 async/await 写法,更清晰地处理异步流程和错误捕获:
const scrape = require('aliexpress-product-scraper'); async function fetchProduct() { try { const result = await scrape('32958933105'); console.log('商品数据:', result); } catch (error) { console.error('抓取出错:', error); } } fetchProduct(); - 确认库的维护状态:如果上述方法都无效,检查 aliexpress-product-scraper 是否已停止维护。若库已废弃,可考虑用 Puppeteer 或 Playwright 自行实现基础的商品详情抓取逻辑。
内容的提问来源于stack exchange,提问作者kimshee16
相关产品推荐
相关产品推荐

