npm安装不成功

npm install puppeteer失败,切换源到中国镜像。

npm config set registry https://registry.npmmirror.com
npm install puppeteer

运行官方案例不成功

image.png
官方给出的引入方式,变更成puppeteer。

import puppeteer from 'puppeteer';
// const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('http://localhost:5173', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});

  await browser.close();
})();

报错 Warning: To load an ES module, set "type": "module"

image.png

(node:19084) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\03 - other\puppeteer\hn.js.js:1

package.json加入type。

{
  "type": "module",
  "devDependencies": {
    "puppeteer": "^23.4.0"
  }
}

报错 ReferenceError: ReadableStream is not defined

image.png
切换node版本到18.

nvm use 18.20.4

终于能运行了。


请鹅出战
328 声望3 粉丝

当我带上墨镜,也许我就真的瞎了。


引用和评论

0 条评论