Accessing files in a worker

Let's say I have files like this in a worker, the data.json file may not always be a JSON file (nothing insane, just some js files or small scripts). I couldn't find any information on how could I access it from the main entrypoint. How could I do this without making a wasteful API request like this: https://developers.cloudflare.com/api/operations/worker-script-download-worker
Cloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
No description
7 Replies
Walshy
Walshy•14mo ago
You can import it as a module e.g import json from './data.json' This will be uploaded with the worker
Erisa
Erisa•14mo ago
This works when wrangler is doing the bundling, but not in quick edit which is what is seemingly used there:
Cannot find module './data.json'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)
Cannot find module './data.json'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)
I know you can import files in quickedit since one of the templates does it with a html file and a js file, but not 100% sure why it hates json specifically
Walshy
Walshy•14mo ago
Oh that's fun
Erisa
Erisa•14mo ago
You can though, do what the playground template does and just have data.js with
export default {
name: "Example API",
tags: ["example", "api", "response"],
};
export default {
name: "Example API",
tags: ["example", "api", "response"],
};
🌈 Mprogramavimai
🌈 MprogramavimaiOP•14mo ago
But let's say it's not even a JS file, but a small database template for example, so there's no way to access it and I basically have to cram it in a JS file?
Hello, I’m Allie!
Only if you use QuickEdit. wrangler can be massaged into accepting any file format you throw at it
🌈 Mprogramavimai
🌈 MprogramavimaiOP•14mo ago
Thanks

Did you find this page helpful?