{"record":{"id":"9448bc4381c50450","repo":"tursodatabase/turso","slug":"panic-mainworker-is-not-initialized-9448bc","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-turbopack-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-turbopack-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-turbopack-hack.ts#L1-L26","documentation":"The turbopack-hack variant (promise-turbopack-hack.ts:8) exists specifically to work around Turbopack's handling of the wasm/worker assets; init() awaits initThreadPool() and then asserts MainWorker (live export of index-turbopack-hack.js) is non-null. A null MainWorker means the setupMainThread callback in that module never created the worker - typically because Turbopack duplicated or mis-resolved the module, or the hack entry is being used under a bundler it was not meant for (or vice versa).","triggerScenarios":"Using the turbopack-hack entry under webpack/Vite (or the default entry under Turbopack), producing a module graph where the instance you import never ran setupMainThread's worker callback; Next.js dev (Turbopack) vs production (webpack) resolving different copies; worker.js/wasm assets missing from the Turbopack build output.","commonSituations":"Next.js apps that work in `next dev` (Turbopack) but throw at build time, or the reverse; entry-point churn during upgrades leaving imports split across variants; monorepo setups pulling two copies of the wasm package.","solutions":["Under Turbopack use the turbopack-hack entry consistently (and only it); under other bundlers use the matching variant.","Await init() once at app startup before creating databases.","Dedupe the package and confirm a single module instance serves all imports.","If dev works but build fails (or vice versa), align the entry variant between dev and prod configs."],"exampleFix":"// before\nimport { Database } from \"@tursodatabase/database-wasm/promise-default\"; // under Turbopack\n\n// after\nimport { init, Database } from \"@tursodatabase/database-wasm/promise-turbopack-hack\";\nawait init();","handlingStrategy":"validation","validationCode":"import { init, Database } from \"@tursodatabase/database-wasm/promise-turbopack-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    // wrong entry variant for this bundler, or duplicate module: align + dedupe\n  } else throw e;\n}","preventionTips":["Use the turbopack-hack entry only under Turbopack, consistently in dev and prod.","Await init() before any Database construction.","Dedupe the package in monorepos to avoid split module instances."],"tags":["wasm","initialization","worker","turbopack","nextjs"],"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"}