Skip to content

Commit 723b8a3

Browse files
docs: add tutorial for cloudflare workers with wrangler
1 parent 2ca25d2 commit 723b8a3

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
last_modified: 2026-04-18
3+
title: "Deploying Deno to Cloudflare Workers with Wrangler"
4+
description: "Learn how to build and deploy a Deno application to Cloudflare Workers using Wrangler"
5+
url: /examples/cloudflare_workers_wrangler_tutorial/
6+
---
7+
8+
Cloudflare Workers allows you to run JavaScript on Cloudflare's edge network.
9+
10+
This is a short How To guide on deploying a Deno function to Cloudflare Workers.
11+
If you are looking to build a standard Deno web server instead, check out the
12+
[HTTP Server tutorial](/examples/http_server/). If you want to use third-party
13+
tool `denoflare`, refer to
14+
[Cloudflare Workers tutorial](/examples/cloudflare_workers_tutorial/).
15+
16+
Note: You would only be able to deploy
17+
[Module Workers](https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/)
18+
instead of web servers or apps.
19+
20+
## Setup wrangler
21+
22+
First, initialize your project and add Cloudflare's `wrangler` CLI as an npm
23+
dependency. Deno's native npm support makes this seamless.
24+
25+
```shell
26+
deno add npm:wrangler
27+
```
28+
29+
Next, configure your Cloudflare project by creating a `wrangler.json` file in
30+
the root directory. We'll set the entry point to a `src/mod.ts` file we will
31+
create later.
32+
33+
```json
34+
{
35+
"$schema": "https://www.unpkg.com/wrangler@4.38.0/config-schema.json",
36+
"name": "deno-wrangler",
37+
"main": "src/mod.ts",
38+
"compatibility_date": "2026-04-18",
39+
"observability": {
40+
"enabled": true
41+
}
42+
}
43+
```
44+
45+
Update your `deno.json` file to include helpful tasks for local development and
46+
deployment. We can invoke Wrangler directly through Deno.
47+
48+
```json
49+
{
50+
"tasks": {
51+
"deploy": "deno --allow-env --allow-run wrangler deploy",
52+
"dev": "deno wrangler dev",
53+
"start": "deno wrangler dev",
54+
"cf-typegen": "deno wrangler types"
55+
}
56+
}
57+
```
58+
59+
Run the type generation task to create Cloudflare environment types so your
60+
TypeScript compiler understands the Cloudflare context.
61+
62+
```shell
63+
deno task cf-typegen
64+
```
65+
66+
## Create your function
67+
68+
Now, create your worker script in `src/mod.ts`. It needs to export an object
69+
containing a `fetch` handler to satisfy the Cloudflare Module Worker API.
70+
71+
```typescript
72+
export default {
73+
async fetch(req) {
74+
return new Response("Hello World");
75+
},
76+
} satisfies ExportHandler<Env>;
77+
```
78+
79+
## Setup build
80+
81+
Because Cloudflare Workers runs raw JavaScript and needs standard module
82+
resolution, we must bundle our Deno code (and resolve Deno-specific imports)
83+
before deploying when we depend on external libraries. We will use `esbuild` for
84+
this. For a deeper dive into how this works, see the
85+
[esbuild tutorial](https://www.google.com/search?q=/examples/esbuild/).
86+
87+
Add `esbuild` and the official Deno esbuild plugin to your dependencies:
88+
89+
```shell
90+
deno add npm:esbuild jsr:@deno/esbuild-plugin
91+
```
92+
93+
Create a `build.ts` script in your root directory. This script will bundle your
94+
`src/mod.ts` file into a single JavaScript file at `dist/server.js`.
95+
96+
```typescript
97+
import * as esbuild from "esbuild";
98+
import { denoPlugin } from "@deno/esbuild-plugin";
99+
100+
await esbuild.build({
101+
entryPoints: ["./src/mod.ts"],
102+
outfile: "./dist/server.js",
103+
format: "esm",
104+
bundle: true,
105+
minify: true,
106+
treeShaking: true,
107+
plugins: [
108+
denoPlugin(),
109+
],
110+
});
111+
112+
await esbuild.stop();
113+
```
114+
115+
Finally, add a build task to your `deno.json` (e.g.,
116+
`"build": "deno run -A build.ts"`)
117+
118+
```json
119+
{
120+
"tasks": {
121+
"build": "deno run -REW --allow-run build.ts"
122+
}
123+
}
124+
```
125+
126+
and update your `wrangler.json` to instruct Wrangler to execute this build step
127+
prior to deployment. Make sure your `main` field points to the bundled output
128+
(`dist/server.js`).
129+
130+
```json
131+
{
132+
"$schema": "https://www.unpkg.com/wrangler@4.38.0/config-schema.json",
133+
"name": "deno-wrangler",
134+
"main": "dist/server.js",
135+
"compatibility_date": "2026-04-18",
136+
"observability": {
137+
"enabled": true
138+
},
139+
"build": {
140+
"command": "deno task build"
141+
}
142+
}
143+
```
144+
145+
## Deploy
146+
147+
With everything configured, deploying to Cloudflare's edge network is just one
148+
command away.
149+
150+
Run the deploy task we set up in our deno.json:
151+
152+
```shell
153+
deno task deploy
154+
```
155+
156+
Wrangler will automatically run your build.ts script to bundle the application,
157+
and then safely publish the resulting dist/server.js file to Cloudflare Workers.
158+
Once finished, Wrangler will output the live URL where your worker is hosted.

0 commit comments

Comments
 (0)