{"record":{"id":"befe29ef0549919e","repo":"mastra-ai/mastra","slug":"chat-module-not-loaded-yet-call-getchatmodule","errorCode":null,"errorMessage":"chat module not loaded yet — call getChatModule() first","messagePattern":"chat module not loaded yet — call getChatModule\\(\\) first","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/channels/chat-lazy.ts","lineNumber":46,"sourceCode":"    return cached;\n  }\n  if (!loading) {\n    loading = (async () => {\n      const chatModule = await import(/* @vite-ignore */ /* webpackIgnore: true */ 'chat');\n      cached = chatModule;\n      return chatModule;\n    })();\n  }\n  return loading;\n}\n\n/**\n * Synchronous accessor for the `chat` module.\n * Only safe to call after `getChatModule()` has been awaited (e.g. after `AgentChannels.initialize()`).\n */\nexport function chatModule(): ChatModule {\n  if (!cached) {\n    throw new Error('chat module not loaded yet — call getChatModule() first');\n  }\n  return cached;\n}\n\nexport type { Chat };\n","sourceCodeStart":28,"sourceCodeEnd":52,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/channels/chat-lazy.ts#L28-L52","documentation":"chatModule() is the synchronous accessor for the lazily-loaded `chat` module. The module is only populated after awaiting getChatModule(). This guard Error prevents using the sync accessor before the async load completed. It is an initialization-order contract enforced at runtime.","triggerScenarios":"Calling chatModule() before any code has awaited getChatModule() — e.g. at module top level, or in code paths that run before AgentChannels.initialize() (or equivalent) has resolved the dynamic import.","commonSituations":"Importing a helper that synchronously calls chatModule() during app bootstrap; calling chat functions before initializing AgentChannels; top-level module code that touches chat APIs without awaiting the loader.","solutions":["Await getChatModule() once during startup (e.g. in AgentChannels.initialize()) before any chatModule() call.","Replace top-level chatModule() usage with the async getChatModule() flow.","Move chat-dependent initialization into an async init function that first awaits getChatModule()."],"exampleFix":"// before\nimport { chatModule } from './chat-lazy';\nconst chat = chatModule(); // throws at import time\n// after\nimport { getChatModule, chatModule } from './chat-lazy';\nawait getChatModule();\nconst chat = chatModule();","handlingStrategy":"validation","validationCode":"import { getChatModule } from './chat-lazy';\nexport async function ensureChatReady() { await getChatModule(); } // call once at startup before any sync accessor use","typeGuard":"null","tryCatchPattern":"try { const m = chatModule(); } catch { await getChatModule(); const m = chatModule(); }","preventionTips":["Await getChatModule() in AgentChannels.initialize() or app bootstrap before exposing sync chat APIs","Never call chatModule() at module top level","Centralize initialization behind a single ensureReady() guard"],"tags":["lazy-loading","initialization-order","channels"],"backgroundTag":"module-not-initialized","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}