{"record":{"id":"d33d5e644c8abaae","repo":"vitest-dev/vitest","slug":"vitest-importscripts-is-not-supported-in-vite","errorCode":null,"errorMessage":"[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.","messagePattern":"\\[vitest\\] `importScripts` is not supported in Vite workers\\. Please, consider using `import` instead\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/web-worker/src/runner.ts","lineNumber":40,"sourceCode":"  const evaluator = new VitestModuleEvaluator(vm, {\n    interopDefault: state.config.deps.interopDefault,\n    injectCjsGlobals: state.config.injectCjsGlobals,\n    moduleExecutionInfo: state.moduleExecutionInfo,\n    getCurrentTestFilepath: () => state.filepath,\n    compiledFunctionArgumentsNames,\n    compiledFunctionArgumentsValues,\n  })\n\n  return startVitestModuleRunner({\n    evaluator,\n    evaluatedModules: state.evaluatedModules,\n    mocker,\n    state,\n  })\n}\n\nfunction importScripts() {\n  throw new Error(\n    '[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.',\n  )\n}\n","sourceCodeStart":22,"sourceCodeEnd":44,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/web-worker/src/runner.ts#L22-L44","documentation":"@vitest/web-worker runs workers as ES modules through Vite, so the classic Web Worker importScripts(...) API is fundamentally incompatible. The runner injects a replacement importScripts (runner.ts:39-43) into the worker context that throws immediately, surfacing the incompatibility at the call site instead of failing silently.","triggerScenarios":"Code executed inside a @vitest/web-worker Worker calling importScripts(...) at runtime. The throw fires on the call, inside the worker realm.","commonSituations":"Porting a library built for classic workers; legacy code that still uses importScripts; a third-party script pulled into the worker that calls importScripts; code not yet migrated to ESM.","solutions":["Replace importScripts(url) with a dynamic ESM import: await import(url).","Convert the classic script to an ES module (add export/import statements) and use a static import.","If you only need side effects, use vi.mock or a static import of the module instead of importScripts."],"exampleFix":"// before (inside the worker)\nimportScripts('https://cdn.example/lib.js')\n\n// after\nawait import('https://cdn.example/lib.js')\n// or for local modules: import './lib.js'","handlingStrategy":"type-guard","validationCode":"// Scan worker source before registering it as a @vitest/web-worker.\nfunction workerUsesImportScripts(source) {\n  return /\\bimportScripts\\s*\\(/.test(source)\n}\nif (workerUsesImportScripts(workerSource)) {\n  throw new Error('Worker source uses importScripts; migrate to dynamic import().')\n}","typeGuard":"function isEsmWorkerSource(src: string): boolean {\n  return !/\\bimportScripts\\s*\\(/.test(src)\n}","tryCatchPattern":"// inside the worker module, isolate the legacy call\ntry {\n  // @ts-expect-error legacy global\n  importScripts(url)\n} catch (e) {\n  if (e instanceof Error && /importScripts is not supported/.test(e.message)) {\n    console.warn('Blocked importScripts; migrate this call to: await import(url)')\n  } else {\n    throw e\n  }\n}","preventionTips":["Migrate worker code to ESM (static or dynamic import) before running under @vitest/web-worker.","Audit third-party worker libs for importScripts usage before pulling them in.","Add a lint rule or pre-test grep for importScripts in worker sources.","Prefer static imports where possible; they surface load failures at build time."],"tags":["web-worker","esm","vite","migration"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}