{"record":{"id":"a546639f2e7a211b","repo":"denoland/deno","slug":"cannot-import-scripts-in-a-module-worker","errorCode":null,"errorMessage":"Cannot import scripts in a module worker","messagePattern":"Cannot import scripts in a module worker","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"runtime/js/99_main.js","lineNumber":392,"sourceCode":"      // delivering this already-dequeued message so a handler that re-armed\n      // itself in a microtask after the previous dispatch (e.g. reassigning\n      // `onmessage` inside a `.then`) is installed first -- otherwise the\n      // message reaches the stale handler and is lost. A synchronous\n      // checkpoint can't help: V8 won't run microtasks reentrantly while we\n      // are already inside one.\n      await new Promise((resolve) => queueMicrotask(() => resolve()));\n      if (isClosing) break;\n      op_worker_maybe_wait_for_debugger();\n      dispatchWorkerMessage(syncData);\n    }\n  }\n}\n\nlet loadedMainWorkerScript = false;\n\nfunction importScripts(...urls) {\n  if (op_worker_get_type() !== \"classic\") {\n    throw new TypeError(\"Cannot import scripts in a module worker\");\n  }\n\n  const baseUrl = location.getLocationHref();\n  const parsedUrls = ArrayPrototypeMap(urls, (scriptUrl) => {\n    try {\n      return new url.URL(scriptUrl, baseUrl ?? undefined).href;\n    } catch {\n      throw new DOMException(\n        `Failed to parse URL: ${scriptUrl}`,\n        \"SyntaxError\",\n      );\n    }\n  });\n\n  // A classic worker's main script has looser MIME type checks than any\n  // imported scripts, so we use `loadedMainWorkerScript` to distinguish them.\n  // TODO(andreubotella) Refactor worker creation so the main script isn't\n  // loaded with `importScripts()`.","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/denoland/deno/blob/a961cdec3b1948844414ebeecc697dcad76df2d1/runtime/js/99_main.js#L374-L410","documentation":"importScripts exists only in classic workers; Deno workers default to `type: 'module'`, and importScripts in runtime/js/99_main.js checks op_worker_get_type() on every call, throwing TypeError('Cannot import scripts in a module worker') otherwise. ES modules must be loaded with import statements or dynamic import() instead. Deno itself bootstraps classic workers by generating an importScripts(...) call for the main script, which is why the function exists at all.","triggerScenarios":"Calling importScripts() inside `new Worker(url, { type: 'module' })` or with the default type; running web-worker scripts written for classic workers (common in older libraries) unchanged in Deno.","commonSituations":"Porting classic worker scripts that load UMD/global scripts via importScripts; mixing CDN script-tag style loading into module workers; sharing one worker file between a browser classic context and Deno.","solutions":["Replace importScripts with a static `import` or `await import(url)` — dynamic import covers runtime-computed URLs","If the script only exists as classic JS, create the worker with `{ type: 'classic' }` (note: classic workers do not get TypeScript/ESM resolution for subsequent imports)","When re-authoring is impossible, fetch the script text and load it via a data: URL import"],"exampleFix":"// before\nimportScripts('https://example.com/lib.js');\n\n// after\nconst lib = await import('https://example.com/lib.js');","handlingStrategy":"fallback","validationCode":"// dual-mode loader that works in classic and module workers\nasync function loadScript(url) {\n  if (typeof importScripts === 'function') {\n    importScripts(url); // classic worker\n  } else {\n    await import(url); // module worker\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to ES module workers in Deno; load code with static or dynamic import","Write worker scripts in one mode — never mix importScripts with import/export syntax","If classic scripts are unavoidable, create the Worker with { type: 'classic' } deliberately"],"tags":["workers","importscripts","esm","modules","typeerror"],"backgroundTag":"importscripts-module-worker","analyzedSha":"a961cdec3b1948844414ebeecc697dcad76df2d1","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}