Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8879

GlitchでPuppeteerを使う際の注意(--no-sandbox)

$
0
0
  • Glitch:Webサイト・APIなどをデプロイできるサービス
  • Puppeteer:ヘッドレスブラウザを立ち上げてスクレイピングなどに使えるNodeライブラリ。GoogleのChrome DevToolsチームが開発してる

ローカルのNodeで開発したPuppeteer利用APIをGlitchにデプロイして起きたエラーの解消メモ

コード(失敗時)

一般的な以下のような書き方で実行すると…

constpuppeteer=require('puppeteer')(async()=>{constbrowser=awaitpuppeteer.launch()constpage=awaitbrowser.newPage()awaitpage.goto('https://example.com')awaitpage.screenshot({path:'example.png'})awaitbrowser.close()})()

エラー

次のようなエラーがログに吐き出される

UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using --no-sandbox.

コード(成功時)

エラーメッセージに出ているように、--no-sandboxオプションをつけてpuppeteer.launch()すればOKなので

(async()=>{constbrowser=awaitpuppeteer.launch({args:['--no-sandbox']})constpage=awaitbrowser.newPage()awaitpage.goto('https://example.com')awaitpage.screenshot({path:'example.png'})awaitbrowser.close()})()

で無事実行可能に。Glitchのディスカッションをみると、Glitchの各プロジェクトはDockerコンテナ内で実行されるためsandboxをrunできない、とのことだった


Viewing all articles
Browse latest Browse all 8879

Trending Articles