Closed2

Cloudflare Workers(Pages Function)の躓き

yutak23yutak23

Node.jsの標準モジュールに依存する場合は、工夫が必要になる

Cloudflare adapter: Could not resolve "path" and "fs"に書かれている通り、Cloudflare Workerは別にNode.jsの実行環境ではないので、Node.jsの組み込みのモジュール(fspathなど)を利用することはできない(=ブラウザ上でのJavaScriptの実行と同じ環境である)

Cloudflare Workers do not run Node. While it looks weird that the platform being built for is browser, that's practically what it is since Cloudflare Workers run "just" V8, which means that the built-in Node libraries are not there. Changing the platform to node won't make Node libraries available on Workers, so you must change your code not to use Node libraries or packages that depend on them.
(抜粋:Cloudflare Workersは「単に」V8を実行するため、ビルトインのNodeライブラリが存在しない)

ではどうするのか?というと、Node.js compatibilityにあるような対応するか、そもそもNode.jsの標準モジュールを利用しないようにする。

※最初、サーバレスのRedisをioredisというライブラリで利用しようとしていたが、それは間違いで、基本的にエッジ環境ではサーバレスのDB(Upstash RedisPlanetscaleなど)を利用する時も、ライブラリ(ioredis、mysql2など)を利用せず、HTTPリクエストでクエリを投げる実装になる。

yutak23yutak23

subrequestの制限

Workerのエンドポイントに対する1回のリクエストがあった時、そのエンドポイントの中の実装でほかのAPIを呼び出すような実装をしている場合には、他のAPIの呼び出し回数には上限があり50回と決まっている。

そのため、例えばバッチ処理を書きたくなくて、APIの中で時間が少しかかってもいいからやろう、なんていうことはできない。

https://developers.cloudflare.com/workers/platform/limits#account-plan-limits

このスクラップは2024/01/19にクローズされました