workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

[code 10002] Error deploying worker built with vite

Open mmakhalaf opened this issue 10 months ago • 6 comments

What versions & operating system are you using?

Wrangler v4.18.0, Node v20.18.2, Windows 11

Please provide a link to a minimal reproduction

No response

Describe the Bug

I'm getting this error when running vite build followed by wrangler deploy. I would like to know if there is a way to find out more information about the error to try to pin it down please...

X [ERROR] A request to the Cloudflare API (/accounts/<account_id>/workers/scripts/<worker_name>/versions) failed.

  An unknown error has occurred. If this error persists, please file a report in the workers-sdk
  GitHub repository (https://github.com/cloudflare/workers-sdk/issues) or reach out to your account
  team. [code: 10002]

Please provide any relevant error logs

No response

mmakhalaf avatar Jun 02 '25 17:06 mmakhalaf

~We just started hitting this issue today as well.~ EDIT: My error is related to API calls to the workers route API failing. Code deployments are unaffected, so these might be unrelated issues.

This has completely blocked our workers release pipeline.

❯ npx wrangler deploy --config=wrangler-staging.toml 

 ⛅️ wrangler 4.18.0
[...]

✘ [ERROR] A request to the Cloudflare API (/accounts/[REDACTED]/workers/scripts/[REDACTED]routes) failed.

  An unknown error has occurred. If this error persists, please file a report in workers-sdk
  (https://github.com/cloudflare/workers-sdk/issues) or reach out to your account team. [code:
  10013]
  
  If you think this is a bug, please open an issue at:
  https://github.com/cloudflare/workers-sdk/issues/new/choose

As a side note, there should be a flag for verbose/debug logs to help troubleshoot issues like this.

devonhk avatar Jun 02 '25 21:06 devonhk

Update: I have a file which dynamically imports another file which imports express. This code never runs so express should never be imported in theory, and it isn't a runtime issue locally (shows some warnings though) but perhaps the dynamic import (and resulting assets/* scripts containing express) is causing an issue.

mmakhalaf avatar Jun 02 '25 21:06 mmakhalaf

you can see debug logs by setting the environment variables WRANGLER_LOG=debug and WRANGLER_LOG_SANITIZE=false - if you could paste them here we can have a look. Also, if you're able to provide a minimal reproduction, that'd also help us diagnose the issue :)

emily-shen avatar Jun 03 '25 09:06 emily-shen

@devonhk can you please file a separate issue? since you're running into problems with a different endpoint, it's likely a different issue, and it will help us track resolution if it has its own ticket. if you are able, can you also please include a minimal reproduction? this will help us diagnose the issue. thank you!

lrapoport-cf avatar Jun 09 '25 13:06 lrapoport-cf

same issue for me, also linked to a community post: https://community.cloudflare.com/t/a-request-to-the-cloudflare-api-failed/809429

moesmufti avatar Jun 19 '25 14:06 moesmufti

I have actually just now fixed it.

The deploy fails (for me) because the generated Worker’s **main module (dist//index.js) is well above Cloudflare’s 1 MB per-module hard limit for Module-format Workers.

Wrangler bundles everything (plus the Node-compat polyfills) into that single file, so the API rejects the upload and returns the opaque 500 / code 10002 error.

Fix: make Vite split the Worker build into smaller chunks before Wrangler uploads them. You can do this by adding the following to your vite.config.ts

export default defineConfig(( ) => ({
// Other config lines
	build: {
		rollupOptions: {
			output: {
				// Put every top–level node_module into its own chunk.
				// Example:  node_modules/@polar-sh/sdk → chunk “@polar-sh”
				manualChunks(id) {
					if (id.includes("node_modules")) {
						const name = id
							.split("/node_modules/")[1] // strip leading path
							.replace(/^@?([^/]+).*$/, "$1"); // top-level package name
						return name;
					}
				},
			},
		},
	},
}));

moesmufti avatar Jun 19 '25 19:06 moesmufti

So at one point, when I reverted the code and put the flags, I got this error,

X [ERROR] A request to the Cloudflare API (/accounts/<account-id>/workers/scripts/<worker-name>/versions) failed.

  Uncaught TypeError: Cannot read properties of undefined (reading 'node')
    at null.<anonymous> (index.js:45710:47) in requireClsHooked
    at null.<anonymous> (index.js:45722:15) in requireExpressHttpContext
    at null.<anonymous> (index.js:45746:33)
   [code: 10021]

In order to reproduce something like this, I put this code in the worker index file. This may have been the wrong thing to do in the first place, but since our packages has a mixture of express and worker-compatible code, we need a way to catch these issues at build time rather than deploy time (or I guess wherever the error is, for it to be more helpful) ideally.

let Express: any;
if (typeof globalThis.process !== "undefined") {
  // conditionally import express to exclude from cloudflare workers
  import("express")
    .then((module) => {
      Express = module.application;
    })
    .catch(() => {
      console.log("Express is not available in this environment.");
    });
}

I can't really copy the logs here since they seem to contain the code.

mmakhalaf avatar Jun 25 '25 08:06 mmakhalaf

I'm closing this issue until a repro is attached - there is little we can do without that. Feel free to re-open when you attach a repro.

vicb avatar Jun 30 '25 13:06 vicb

Cloudflare’s 1 MB per-module hard limit for Module-format Workers

@moesmufti — this isn't a limit on the Workers platform. Can you say a bit more about how you came to that understanding?

https://developers.cloudflare.com/workers/platform/limits/#worker-size

irvinebroque avatar Aug 15 '25 23:08 irvinebroque