Contents

Cloud function with puppeteer error

Contents
1
2
# cloud function 在使用 puppeteer 遇到這個錯誤
Error: Could not find Chromium (rev. 1108766). This can occur if either
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 解決方法
// 在根目錄建立 .puppeteerrc.cjs 內容如下

const { join } = require("path");

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
  cacheDirectory: join(__dirname, ".cache", "puppeteer"),
};

.gitignore 加上 .cache

package.json加入以下script, 會在cloud function執行前做預處理 https://cloud.google.com/appengine/docs/standard/nodejs/running-custom-build-step?hl=zh-cn#example

1
2
3
4
"scripts": {
  "gcp-build": "node node_modules/puppeteer/install.js",
  /* etc */
}

來源

https://thinkdrastic.net/journal/2023/01/13/using-puppeteer-19-on-google-cloud-functions/