Cannot upload worker script using Multipart upload

What is the error number?

10021

What is the error message?

Uncaught TypeError: Main module name is not present in bundle.\n

What is the issue or error you’re encountering

I’m trying to use the “Upload worker module” API route, but I always have the same error

What steps have you taken to resolve the issue?

Hello

I’m trying to use this API route to upload a worker with metadata.

I’m using Node.js and Axios, with the following code:

  const api = axios.create({
      baseURL: 'https://api.cloudflare.com/client/v4',
      headers: {
        Authorization: `Bearer ${myToken}`,
        'Content-Type': 'multipart/form-data',
      },
  })

  const workerCode = `export default {
  async fetch(request, env, ctx) {
    return new Response("Hello World!");
  },
};`

  const form = new FormData();
  form.append('main.js', workerCode);
  form.append(
    'metadata',
    JSON.stringify({
      main_module: 'main.js',
      compatibility_date: '2025-02-04',
      compatibility_flags: ['nodejs_compat'],
    })
  );

  await api.put(
    `/accounts/${accountId}/workers/scripts/${workerName}`,
    form
  );

The error message is always the same:
Uncaught TypeError: Main module name is not present in bundle

Copy-pasting the worker code manually in the Cloudflare Dashboard works.

What am I missing? I cannot find any example in the documentation, but I carefully read the API docs and I cannot make it work

Thanks a lot for your help

If found the solution thanks to someone one StackOverflow

I was passing the worker code as a string field, but it needed to be a file instead, with a file name!

form.append('main.js', new Blob([workerCode]), 'main.js');

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.