Is it possible to upload a worker script (w/ ESM syntax) using the API?

According to the API docs, uploaded scripts have to be in service worker syntax – and I can confirm this: scripts in ESM syntax can be deployed via the browser editor but not via the API. Is this going to be fixed? Is there a way to work around this? thanks!
5 Replies
kian
kian16mo ago
The API fully supports ESM syntax That's what Wrangler and the dashboard use
cyzyvos
cyzyvosOP16mo ago
Unfortunately, I don't think that's the case. If I try to deploy my script via the API, I get the following error response:
{
result: null,
success: false,
errors: [
{
code: 10021,
message: "Uncaught SyntaxError: Unexpected token 'export'\n at worker.js:432\n"
}
],
messages: []
}
{
result: null,
success: false,
errors: [
{
code: 10021,
message: "Uncaught SyntaxError: Unexpected token 'export'\n at worker.js:432\n"
}
],
messages: []
}
If I paste the exact same code into the dashboard editor, it works. Also, the API doc explicitly states that only the service worker syntax is supported: https://developers.cloudflare.com/api/operations/worker-script-upload-worker-module#request-body
Cloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
Baya
Baya16mo ago
It is possible. I upload workers with multiple modules using the API. I define the modules as separate mutipart/form-data components, define the form boundary manually, manually combine the modules as multipart data with the proper heading for each. You will also need to include a meta-data defining the name of your main module and bindings. It appears the documentation has limited info; I figured it with some exprimentation.
kian
kian16mo ago
No description
cyzyvos
cyzyvosOP15mo ago
thanks @baya_78759 and @kiannh, this worked!
Want results from more Discord servers?
Add your server