当前位置: 代码迷 >> 综合 >> 解决:Error: Cannot find module ‘puppeteer/DeviceDescriptors‘ 问题
  详细解决方案

解决:Error: Cannot find module ‘puppeteer/DeviceDescriptors‘ 问题

热度:11   发布时间:2024-02-20 01:54:11.0

最近在学习 puppeteer ,网上很多的代码其实已经过期了

遇到出下问题:

node .\test.js
internal/modules/cjs/loader.js:1032throw err;^Error: Cannot find module 'puppeteer/DeviceDescriptors'
Require stack:

源代码是这样写的:

const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors')(async () => {
    const browser = await puppeteer.launch();const page = await browser.newPage();await page.emulate(devices['iPhone X'])await page.goto('https://www.baidu.cn');await page.screenshot({
    path: 'examplex.png'});await browser.close();
})();

经过查看官方文档得出正确结论:

const puppeteer = require('puppeteer');(async () => {
    const browser = await puppeteer.launch();const page = await browser.newPage();await page.emulate(puppeteer.devices['iPhone 6']);await page.goto('https://www.baidu.cn');await page.screenshot({
     path: 'full.png', fullPage: true });await browser.close();
})();

已录下,希望能帮助到朋友们

  相关解决方案