Cloudflare Worker Experimental
Elysia now supports Cloudflare Worker with an experimental Cloudflare Worker Adapter.
- You will need Wrangler to setup, and start a development server.
wrangler init elysia-on-cloudflare
- Then add Cloudflare Adapter to your Elysia app, and make sure to call
.compile()
before exporting the app.
import { Elysia } from 'elysia'
import { CloudflareAdapter } from 'elysia/adapter/cloudflare-worker'
export default new Elysia({
adapter: CloudflareAdapter
})
.get('/', () => 'Hello Cloudflare Worker!')
// This is required to make Elysia work on Cloudflare Worker
.compile()
- Make sure to have
compatibility_date
set to at least2025-06-01
in your wrangler config
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "elysia-on-cloudflare",
"main": "src/index.ts",
"compatibility_date": "2025-06-01"
}
main = "src/index.ts"
name = "elysia-on-cloudflare"
compatibility_date = "2025-06-01"
- Now you can start the development server with:
wrangler dev
This should start a development server at http://localhost:8787
You don't need a nodejs_compat
flag as Elysia doesn't use any Node.js built-in modules (or the ones we use don't support Cloudflare Worker yet).
Limitations
Here are some known limitations of using Elysia on Cloudflare Worker:
Elysia.file
, and Static Plugin doesn't work due to the lack offs
module- OpenAPI Type Gen doesn't work due to the lack of
fs
module - You can't define Response before server start, or use plugin that does so
- You can't inline a value
import { Elysia } from 'elysia'
new Elysia()
// This will throw error
.get('/', 'Hello Elysia')
.listen(3000)
Static File
Static Plugin doesn't work, but you can still serve static files with Cloudflare's built-in static file serving.
Add the following to your wrangler config:
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "elysia-on-cloudflare",
"main": "src/index.ts",
"compatibility_date": "2025-06-01",
"assets": { "directory": "public" }
}
name = "elysia-on-cloudflare"
main = "src/index.ts"
compatibility_date = "2025-06-01"
assets = { directory = "public" }
Create a public
folder and place your static files in it.
For example, if you have a folder structure like this:
│
├─ public
│ ├─ kyuukurarin.mp4
│ └─ static
│ └─ mika.webp
├─ src
│ └── index.ts
└─ wrangler.toml
Then you should be able to access your static file from the following path:
Binding
You can use a Cloudflare Workers binding by importing env from cloudflare:workers
.
import { Elysia } from 'elysia'
import { CloudflareAdapter } from 'elysia/adapter/cloudflare-worker'
import { env } from 'cloudflare:workers'
export default new Elysia({
adapter: CloudflareAdapter
})
.get('/', () => `Hello ${await env.KV.get('my-key')}`)
.compile()
See Cloudflare Workers: Binding for more information about binding.
AoT compilation
Previously, to use Elysia on Cloudflare Worker, you have to pass aot: false
to the Elysia constructor.
This is no longer necessary as Cloudflare now supports Function compilation during startup.
As of Elysia 1.4.7, you can now use Ahead of Time Compilation with Cloudflare Worker, and drop the aot: false
flag.
import { Elysia } from 'elysia'
import { CloudflareAdapter } from 'elysia/adapter/cloudflare-worker'
export default new Elysia({
aot: false,
adapter: CloudflareAdapter
})
Otherwise, you can still use aot: false
if you don't want Ahead of Time Compilation but we recommend you to use it for better performance, and accurate plugin encapsulation.