{"record":{"id":"60fd752eac744675","repo":"tursodatabase/turso","slug":"panic-mainworker-is-not-initialized-60fd75","errorCode":null,"errorMessage":"panic: MainWorker is not initialized","messagePattern":"panic: MainWorker is not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"bindings/javascript/packages/wasm/promise-vite-dev-hack.ts","lineNumber":8,"sourceCode":"import { DatabasePromise, DatabaseOpts, SqliteError, Transaction } from \"@tursodatabase/database-common\"\nimport { registerFileAtWorker, unregisterFileAtWorker, ioNotifier } from \"@tursodatabase/database-wasm-common\";\nimport { initThreadPool, MainWorker, Database as NativeDatabase } from \"./index-vite-dev-hack.js\";\n\nasync function init(): Promise<Worker> {\n    await initThreadPool();\n    if (MainWorker == null) {\n        throw new Error(\"panic: MainWorker is not initialized\");\n    }\n    return MainWorker;\n}\n\nclass Database extends DatabasePromise {\n    #worker: Worker | null;\n    constructor(path: string, opts: DatabaseOpts = {}) {\n        super(\n            new NativeDatabase(path, opts) as unknown as any,\n            () => ioNotifier.waitForCompletion(),\n        )\n    }\n    /**\n     * connect database and pre-open necessary files in the OPFS\n     */\n    override async connect() {\n        if (!this.memory) {\n            const worker = await init();","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/javascript/packages/wasm/promise-vite-dev-hack.ts#L1-L26","documentation":"The vite-dev-hack variant (promise-vite-dev-hack.ts:8) compensates for Vite's dev-server module transforms; init() awaits initThreadPool() and throws if MainWorker (live export of index-vite-dev-hack.js) is still null. As with the sibling variants, MainWorker is only assigned when setupMainThread's callback creates the worker inside that exact module instance, so the throw means no worker was registered where you are checking - duplicate module copies, wrong entry for the bundler, or failed asset loading.","triggerScenarios":"Running under Vite dev server but importing a non-hack entry (or production build importing the hack entry), so module transforms split the instance; Vite optimizeDeps re-bundling the wasm package into a second copy; worker.js or turso.wasm not served during dev (import.meta.url base differences); HMR leaving a stale module graph.","commonSituations":"App works after `vite build` but throws in `vite dev` (or the reverse); upgrading Vite changes asset handling and the wasm/worker URLs 404; multiple components importing different entry variants.","solutions":["Use the vite-dev-hack entry for Vite dev and await init() before any Database use.","Exclude @tursodatabase/database-wasm from optimizeDeps (or configure it so only one copy is served).","Confirm turso.wasm32-wasi.wasm and worker.js load in the dev-server network tab.","Standardize on a single entry variant across the app and reload fully after config changes."],"exampleFix":"// before\nimport { Database } from \"@tursodatabase/database-wasm/promise-default\"; // under vite dev\n\n// after\nimport { init, Database } from \"@tursodatabase/database-wasm/promise-vite-dev-hack\";\nawait init();","handlingStrategy":"validation","validationCode":"import { init, Database } from \"@tursodatabase/database-wasm/promise-vite-dev-hack\";\nawait init();\nconst db = new Database(\"app.db\");","typeGuard":null,"tryCatchPattern":"try {\n  await init();\n} catch (e) {\n  if (e instanceof Error && /MainWorker is not initialized/.test(e.message)) {\n    // Vite dev duplicated or mis-served the module: check optimizeDeps + asset URLs\n  } else throw e;\n}","preventionTips":["Use the vite-dev-hack entry for Vite dev and await init() first.","Exclude the wasm package from optimizeDeps so only one copy is served.","Verify worker.js/turso.wasm requests succeed in the dev server network tab.","Standardize one entry variant across the app."],"tags":["wasm","initialization","worker","vite","dev-server"],"backgroundTag":"module-not-initialized","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}