Node.js 18のランタイムでは動かないので、Node.js 16を選択する。メモリは2GB以上を選ぶ。
https://issuetracker.google.com/issues/266279679
宗教上の理由でNode.js 18、Node.js 19を使用する必要がある場合は、
Cloud Functions 第2世代もしくはCloud Runをご使用ください
第1世代のNode.js 18で動かす場合
環境変数の設定が必要かもです
PUPPETEER_CACHE_DIR=/tmp/.cache/puppeteer
index.js
const puppeteer = require('puppeteer')
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = async (req, res) => {
await puppeteer.createBrowserFetcher().download(puppeteer.PUPPETEER_REVISIONS.chromium)
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://example.com/')
res.send(await page.content())
await browser.close()
}
package.json
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"puppeteer": "^19.7.5"
}
}
require()
ではなくimport
を使いたい方はこちら
参考
よくあるエラー
Error: Could not find Chromium (rev. 1095492). This can occur if either
Error: Could not find Chromium (rev. 1095492). This can occur if either
1. you did not perform an installation before running the script (e.g. `npm install`) or
2. your cache path is incorrectly configured (which is: /workspace/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
at ChromeLauncher.resolveExecutablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:127:27)
at ChromeLauncher.executablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:206:25)
at ChromeLauncher.launch (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:93:37)
at async exports.helloWorld (/workspace/index.js:12:19)
Error: Could not find Chromium (rev. 1095492). This can occur if either
1. you did not perform an installation before running the script (e.g. `npm install`) or
2. your cache path is incorrectly configured (which is: /root/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
at ChromeLauncher.resolveExecutablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:127:27)
at ChromeLauncher.executablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:206:25)
at ChromeLauncher.launch (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:93:37)
at async exports.helloWorld (/workspace/index.js:12:19)
Error: ENOENT: no such file or directory, mkdir '/www-data-home/.cache'
/workspace/node_modules/puppeteer-core/.local-chromium/linux-1045629/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
/workspace/node_modules/puppeteer-core/.local-chromium/linux-1045629/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:299:20)
at Interface.<anonymous> (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:287:24)
at Interface.emit (node:events:525:35)
at Interface.emit (node:domain:552:15)
at Interface.close (node:internal/readline/interface:536:10)
at Socket.onend (node:internal/readline/interface:262:10)
at Socket.emit (node:events:525:35)
at Socket.emit (node:domain:552:15)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)