vitest-dev/vitest · error · Error
[vitest] `importScripts` is not supported in Vite workers…
Error message
[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.
What it means
@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.
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.
Example fix
// before (inside the worker)
importScripts('https://cdn.example/lib.js')
// after
await import('https://cdn.example/lib.js')
// or for local modules: import './lib.js' Defensive patterns
Strategy: type-guard
Validate before calling
// Scan worker source before registering it as a @vitest/web-worker.
function workerUsesImportScripts(source) {
return /\bimportScripts\s*\(/.test(source)
}
if (workerUsesImportScripts(workerSource)) {
throw new Error('Worker source uses importScripts; migrate to dynamic import().')
} Type guard
function isEsmWorkerSource(src: string): boolean {
return !/\bimportScripts\s*\(/.test(src)
} Try / catch
// inside the worker module, isolate the legacy call
try {
// @ts-expect-error legacy global
importScripts(url)
} catch (e) {
if (e instanceof Error && /importScripts is not supported/.test(e.message)) {
console.warn('Blocked importScripts; migrate this call to: await import(url)')
} else {
throw e
}
} Prevention
- 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.
When it happens
Trigger: Code executed inside a @vitest/web-worker Worker calling importScripts(...) at runtime. The throw fires on the call, inside the worker realm.
Common situations: 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.
Related errors
- Benchmark provider loaded from
- --cache.dir is deprecated
- Cannot find environment for
- Cannot import " ": its vm context was torn down.
- Cannot spy on export
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/d33d5e644c8abaae.
Report an issue: GitHub.
Appendix: source
Thrown at packages/web-worker/src/runner.ts:40
const evaluator = new VitestModuleEvaluator(vm, {
interopDefault: state.config.deps.interopDefault,
injectCjsGlobals: state.config.injectCjsGlobals,
moduleExecutionInfo: state.moduleExecutionInfo,
getCurrentTestFilepath: () => state.filepath,
compiledFunctionArgumentsNames,
compiledFunctionArgumentsValues,
})
return startVitestModuleRunner({
evaluator,
evaluatedModules: state.evaluatedModules,
mocker,
state,
})
}
function importScripts() {
throw new Error(
'[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.',
)
}
View on GitHub (pinned to 1fa9837ec2)