Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
How do you use ES6 exports in Cloudflare worker scripts?

I'm trying to get familiar with CloudFlare workers.

I've copied a quickstart project from https://developers.cloudflare.com/workers/get-started/quickstarts, via:

wrangler generate my-app https://github.com/cloudflare/worker-template

This gives me a JS file that listens for "fetch" and returns a Response via handleRequest:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello worker!', {
    headers: { 'content-type': 'text/plain' },
  })
}

Ok, that makes sense.

Now I want to include one of Cloudflare's examples

export default {
  fetch() {
    const html = `<!DOCTYPE html>
      <body>
        <h1>Hello World</h1>
        <p>This markup was generated by a Cloudflare Worker.</p>
      </body>
    `;
    return new Response(html, {
      headers: {
        "content-type": "text/html;charset=UTF-8",
      },
    });
  },
};

This makes less sense. Am I supposed to replace all of the original code with that? If I do, I get an error on wrangler publish:

Error: Something went wrong with the request to Cloudflare... No event handlers were registered. This script does nothing.

Am I supposed to mash them together somehow? How? Is there a step or configuration I'm missing?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The two code snippets in your question use two different syntaxes supported by Workers: Service Workers syntax and ES Modules syntax. addEventListener is used with Service Workers syntax, which was the original syntax supported by Workers. More recently, Workers added support for a new ES-modules-based syntax, which uses export default to export handlers. This new syntax avoids some boilerplate (no weird event.respondWith()) and has some other advantages.

You need to specify in your wrangler.toml which syntax you are using. To use modules syntax, you'll need to add:

[build.upload]
format = "modules"
main = "./worker.mjs"

See more documentation here.

about 4 years ago · Juan Pablo Isaza Report

0

Yes, you are supposed to mash them together. Putting it simply according to the guide here

Fundamentally, a Workers application consists of two parts:

An event listener that listens for FetchEvents, and An event handler that returns a Response object which is passed to the event’s .respondWith() method.

Just editing the first example so you understand easily

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const html = `<!DOCTYPE html>
  <body>
    <h1>Hello World</h1>
    <p>This markup was generated by a Cloudflare Worker.</p>
  </body>`;
  return new Response(html, {
    headers: { "content-type": "text/html;charset=UTF-8" },
  })
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!