【node.js】Puppeteerを使用時のエラーについて!俺用メモ!

スクレイピングや、SPAサイトのテストをする際に便利なPuppeteerを使用した際に出たエラーについてメモします。

スポンサーリンク




Puppeteerとは?

PuppeteerはDevToolsプロトコルを介してChromeまたはChromiumを制御するための高レベルAPIを提供するノードライブラリです。

puppeteer.launch() エラー①

・エラーメッセージ

/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:286
            reject(new Error([
                   ^

Error: Failed to launch the browser process! spawn /node_modules/puppeteer/.local-chromium/linux-1002410/chrome-linux/chrome ENOENT


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:286:20)
    at ChildProcess. (/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRun
ner.js:280:24)
    at ChildProcess.emit (node:events:526:28)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
    at onErrorNT (node:internal/child_process:478:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

・修正方法

・Dockerfileに下記追加(chromiumで/usr/bin/chromium-browserが配置されるはず。)

RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      yarn

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
    PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

※PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browserを環境変数に設定しないと下記Pathのchromeプログラムが実行されてしまう。おそらくこれが使えない。。

/node_modules/puppeteer/.local-chromium/linux-1002410/chrome-linux/chrome

環境変数設定しても同じPathを見に行く場合はプログラムを下記に修正。

const browser = await puppeteer.launch({ executablePath: '/path/to/Chrome' });

 ※executablePathの引数を追加する

puppeteer.launch() エラー②

・エラーメッセージ

/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:286
            reject(new Error([
                   ^

Error: Failed to launch the browser process!
[0622/074837.958913:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not su
pported. See https://crbug.com/638180.


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/src/laratest/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:286:20)
    at Interface. (/src/laratest/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner
.js:274:24)
    at Interface.emit (node:events:538:35)
    at Interface.close (node:readline:586:8)
    at Socket.onend (node:readline:277:10)
    at Socket.emit (node:events:538:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

・修正方法

const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disabled-setuid-sandbox'});

参考文献

puppeteer github
puppeteer troubleshooting

以上、よろしくお願いいたします。

スポンサーリンク




シェアする

  • このエントリーをはてなブックマークに追加

フォローする

スポンサーリンク




%d人のブロガーが「いいね」をつけました。