Skip to content

Commit 5d52b9c

Browse files
Add tutorials for cloudflare workers with wrangler (#3055)
1 parent 093dcaf commit 5d52b9c

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

examples/_data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,11 @@ export const sidebar = [
557557
href: "/examples/cloudflare_workers_tutorial/",
558558
type: "tutorial",
559559
},
560+
{
561+
title: "Cloudflare workers with wrangler",
562+
href: "/examples/cloudflare_workers_wrangler_tutorial/",
563+
type: "tutorial",
564+
},
560565
{
561566
title: "Digital Ocean",
562567
href: "/examples/digital_ocean_tutorial/",
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
Wrangler is the official CLI for Node.js provided by Cloudflare for its
11+
developer platform. In this tutorial, we will deploy a Cloudflare Worker by
12+
using Wrangler from Deno.
13+
14+
Denoflare, a Deno-friendly third-party tool, is guaranteed to run on Deno, but
15+
unlike Wrangler, there is no guarantee that it can correctly utilize Cloudflare
16+
features. If you want to use `denoflare`, refer to
17+
[Cloudflare Workers tutorial](/examples/cloudflare_workers_tutorial/).
18+
19+
## Setup wrangler
20+
21+
First, add the wrangler npm module to your project.
22+
23+
```shell
24+
deno add npm:wrangler
25+
```
26+
27+
Next, create `wrangler.json`. Since the wrangler initialization script generates
28+
many files that are only necessary for Node.js, we will write it manually.
29+
Because the JSON schema references `node_modules` by default, we will configure
30+
it to reference the schema from unpkg.com instead.
31+
32+
```json
33+
{
34+
"$schema": "https://www.unpkg.com/wrangler/config-schema.json",
35+
"name": "deno-wrangler",
36+
"main": "src/mod.ts",
37+
"compatibility_date": "2026-04-18",
38+
"observability": {
39+
"enabled": true
40+
}
41+
}
42+
```
43+
44+
Also, update `deno.json` to add commands that invoke wrangler features. The
45+
`dev` and `start` commands overlap, but this is to align with what wrangler
46+
generates for Node.js environment.
47+
48+
```json
49+
{
50+
"tasks": {
51+
"deploy": "deno --allow-env --allow-run npm:wrangler deploy",
52+
"dev": "deno npm:wrangler dev",
53+
"start": "deno npm:wrangler dev",
54+
"cf-typegen": "deno npm:wrangler types"
55+
}
56+
}
57+
```
58+
59+
Finally, execute the `cf-typegen` command to automatically generate the type
60+
definition file, `worker-configuration.d.ts`.
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 which
70+
is generated as `worker-configuration.d.ts` in the previous step.
71+
72+
```typescript
73+
export default {
74+
async fetch(req) {
75+
return new Response("Hello World");
76+
},
77+
} satisfies ExportedHandler<Env>;
78+
```
79+
80+
## Setup build
81+
82+
Wrangler includes a built-in build system powered by esbuild, but it is
83+
configured for Node.js by default. Since this is incompatible with Deno's module
84+
resolution mechanism, you must set up your own build environment and configure
85+
[Custom Builds](https://developers.cloudflare.com/workers/wrangler/custom-builds/).
86+
87+
First, download esbuild and the official Deno esbuild plugin.
88+
89+
```shell
90+
deno add npm:esbuild jsr:@deno/esbuild-plugin
91+
```
92+
93+
Next, create a build script in build.ts. This will bundle src/mod.ts into a
94+
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+
Then, we will set up the build script execution task in `deno.json`.
116+
117+
```json
118+
{
119+
"tasks": {
120+
"build": "deno run -REW --allow-run build.ts"
121+
}
122+
}
123+
```
124+
125+
Finally, edit wrangler.json to set it to run the build task during deployment,
126+
and configure the output file to be the entry point.
127+
128+
```json
129+
{
130+
"$schema": "https://www.unpkg.com/wrangler/config-schema.json",
131+
"name": "deno-wrangler",
132+
"main": "dist/server.js",
133+
"compatibility_date": "2026-04-18",
134+
"observability": {
135+
"enabled": true
136+
},
137+
"build": {
138+
"command": "deno task build"
139+
}
140+
}
141+
```
142+
143+
## Deploy
144+
145+
Once you have finished configuring wrangler, you can deploy your worker to the
146+
Cloudflare edge network by executing the `deploy` command.
147+
148+
```shell
149+
deno task deploy
150+
```
151+
152+
Wrangler will automatically run your build.ts script to bundle the application,
153+
and then safely publish the resulting dist/server.js file to Cloudflare Workers.
154+
Once finished, Wrangler will output the live URL where your worker is hosted.

0 commit comments

Comments
 (0)