Cloud FunctionsでPuppeteer v19を動かす

Node.js 18のランタイムでは動かないので、Node.js 16を選択する。メモリは2GB以上を選ぶ。
https://issuetracker.google.com/issues/266279679
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/ce01ed12-1128-0256-156c-101ebe8cbfd5.png)

:::note info
宗教上の理由でNode.js 18、Node.js 19を使用する必要がある場合は、
**Cloud Functions 第2世代**もしくは**Cloud Run**をご使用ください
:::



<details><summary>第1世代のNode.js 18で動かす場合</summary><div>

環境変数の設定が必要かもです
```bash
PUPPETEER_CACHE_DIR=/tmp/.cache/puppeteer
```
</div></details>


```javascript: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()
}
```

```json:package.json
{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "puppeteer": "^19.7.5"
  }
}
```

`require()`ではなく`import`を使いたい方はこちら

https://qiita.com/7mpy/items/71bcbace1b92aefe1454

# 参考

https://www.chikach.net/category/useful/puppeteer-v19-cloud-functions-workaround/

# よくあるエラー

## Error: Could not find Chromium (rev. 1095492). This can occur if either

```bash
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)
```

```bash
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

```bash
/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)
```