Migrating an old worker to module format

Hi, as far as the worker code goes, I have updated my old worker to use the module format, but I don't understand how to tell CF it is now a module format worker. I haven't used Wrangler to create the worker, it was done via the dashboard
No description
15 Replies
prodigy-anthill
prodigy-anthillOP4mo ago
My worker only have a single file in the editor (see image)
prodigy-anthill
prodigy-anthillOP4mo ago
When I create a new worker in module format (default) it has more files in the editor:
No description
prodigy-anthill
prodigy-anthillOP4mo ago
Anyone know how to upgrade an existing worker to use module format ?
Hard@Work
Hard@Work4mo ago
It should just auto-detect the change
prodigy-anthill
prodigy-anthillOP4mo ago
Thanks for replying. It doesn't look like it auto-detected anything
prodigy-anthill
prodigy-anthillOP4mo ago
Only indication I have that it's not on module format is this dropdown:
No description
Hard@Work
Hard@Work4mo ago
Does that say anything about your selected syntax?
prodigy-anthill
prodigy-anthillOP4mo ago
Yes
prodigy-anthill
prodigy-anthillOP4mo ago
No description
Hard@Work
Hard@Work4mo ago
What if you deploy it? Detecting might occur on deploy
prodigy-anthill
prodigy-anthillOP4mo ago
I can't deploy it because
No description
prodigy-anthill
prodigy-anthillOP4mo ago
This is because it doesn't understand that the worker now has this syntax:
export default {
async fetch(request, env) {
}
}
export default {
async fetch(request, env) {
}
}
So it's kinda stuck on the old syntax I did try to create a new worker, it was created with the module syntax and that was working okay, but looks like the old worker is stuck on the worker syntax I tried working with wrangler on the project by running
npx create-cloudflare@latest images-dev --existing-script images-dev
npx create-cloudflare@latest images-dev --existing-script images-dev
And this is the toml file: (some identifiers renamed)
name = "images-dev"
main = "src/index.js"
compatibility_date = "2024-07-28"
workers_dev = true

[[routes]]
pattern = "https://images-dev.example.com/*"
zone_name = "example.com"

[[routes]]
pattern = "images-dev.example.com"
zone_name = "example.com"
custom_domain = true

[limits]
cpu_ms = 50

[[kv_namespaces]]
id = "..."
binding = "ENV"
name = "images-dev"
main = "src/index.js"
compatibility_date = "2024-07-28"
workers_dev = true

[[routes]]
pattern = "https://images-dev.example.com/*"
zone_name = "example.com"

[[routes]]
pattern = "images-dev.example.com"
zone_name = "example.com"
custom_domain = true

[limits]
cpu_ms = 50

[[kv_namespaces]]
id = "..."
binding = "ENV"
And package.json
{
"name": "images-dev",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.60.3"
}
}
{
"name": "images-dev",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.60.3"
}
}
Hard@Work
Hard@Work4mo ago
If you do a deploy there(with the module syntax script), does it show up correctly in the dash?
prodigy-anthill
prodigy-anthillOP4mo ago
Haven't tried that, will try to deploy from wrangler It seems that has done the trick and now it is a module syntax worker... Now I just need to understand why my worker is not working, but that might have to do with the changes to the code I did It did add some code for me, for example, first line of the worker is
// src/index.js
// src/index.js
And last line is now
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map
I didn't add these... Actually, it did more than that, looks like it's trying to add some es compatibiilty
// src/index.js
var src_default = {
async fetch(request, env) {
// ...
}
}
export {
src_default as default
};
//# sourceMappingURL=index.js.map
// src/index.js
var src_default = {
async fetch(request, env) {
// ...
}
}
export {
src_default as default
};
//# sourceMappingURL=index.js.map
Hard@Work
Hard@Work4mo ago
That's wrangler bundling your code
Want results from more Discord servers?
Add your server