{"record":{"id":"4a5d319bd46d3372","repo":"slidevjs/slidev","slug":"slidev-internal-error-script-setup-block-not","errorCode":null,"errorMessage":"[Slidev] Internal error: <script setup> block not found in slide ${index + 1}.","messagePattern":"\\[Slidev\\] Internal error: <script setup> block not found in slide (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/slidev/node/vite/layoutWrapper.ts","lineNumber":40,"sourceCode":"        if (!match)\n          return\n        const [, no, type] = match\n        if (type !== 'md')\n          return\n        const index = +no - 1\n        const layouts = await utils.getLayouts()\n        const rawLayoutName = data.slides[index]?.frontmatter?.layout ?? data.slides[0]?.frontmatter?.defaults?.layout\n        let layoutName = rawLayoutName || (index === 0 ? 'cover' : 'default')\n        if (!layouts[layoutName]) {\n          console.error(red(`\\nUnknown layout \"${bold(layoutName)}\".${yellow(' Available layouts are:')}`)\n            + Object.keys(layouts).map((i, idx) => (idx % 3 === 0 ? '\\n    ' : '') + gray(i.padEnd(15, ' '))).join('  '))\n          console.error()\n          layoutName = 'default'\n        }\n\n        const setupTag = code.match(RE_SCRIPT_SETUP_TAG)\n        if (!setupTag)\n          throw new Error(`[Slidev] Internal error: <script setup> block not found in slide ${index + 1}.`)\n\n        const templatePart = code.slice(0, setupTag.index!)\n        const scriptPart = code.slice(setupTag.index!)\n\n        const bodyStart = templatePart.indexOf('<template>') + 10\n        const bodyEnd = templatePart.lastIndexOf('</template>')\n        let body = code.slice(bodyStart, bodyEnd).trim()\n        if (body.startsWith('<div>') && body.endsWith('</div>'))\n          body = body.slice(5, -6)\n\n        return [\n          templatePart.slice(0, bodyStart),\n          `<InjectedLayout v-bind=\"_frontmatterToProps($frontmatter,${index})\">\\n${body}\\n</InjectedLayout>`,\n          templatePart.slice(bodyEnd),\n          scriptPart.slice(0, setupTag[0].length),\n          `import InjectedLayout from \"${toAtFS(layouts[layoutName])}\"`,\n          templateImportContextUtils,\n          templateInitContext,","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/slidevjs/slidev/blob/0d798ace5828966d9f77f62094d5f7a971ad98f7/packages/slidev/node/vite/layoutWrapper.ts#L22-L58","documentation":"This is an internal error thrown by Slidev's `slidev:layout-wrapper` Vite plugin when transforming a compiled markdown slide. After resolving the layout, the plugin regex-matches `/^<script setup.*>/m` (RE_SCRIPT_SETUP_TAG) against the slide's transformed code; every slide is expected to already contain a `<script setup>` block injected by an upstream pipeline stage. The throw fires only when that contract is broken — the slide reached the layout wrapper without a script-setup block, so the plugin cannot safely splice in the layout import and context setup it appends.","triggerScenarios":"Fires inside the plugin's `transform.handler` for any module id matching `regexSlideSourceId` with type `md`. Concretely: `code.match(RE_SCRIPT_SETUP_TAG)` returns null. This happens when an earlier transform in the pipeline that is supposed to prepend the `<script setup>` block did not run, ran out of order, or was overridden by a custom user Vite plugin that rewrites/strips the slide module output before the layout wrapper sees it.","commonSituations":"Mismatched @slidev package versions (e.g. @slidev/client or @slidev/parser newer/older than @slidev/parser that emits the script setup), a custom Vite plugin in `vitePlugins`/`unocss` config that transforms `*.md` ids and clobbers the `<script setup>` tag, or a fork/patch of Slidev whose markdown-to-SFC compiler no longer emits the tag. The error names the offending slide by 1-based index (`index + 1`).","solutions":["This is an internal invariant — file a Slidev bug report with the slide content and the offending slide number from the message.","Verify all @slidev/* packages resolve to the same version (run the version check / dedupe node_modules); a mixed-version install is the most common root cause.","Audit any custom Vite plugins you added via `vitePlugins` or Slidev options for a `transform` hook that handles `*.md` ids and may strip or replace the `<script setup>` block before the layout-wrapper plugin runs.","Disable third-party Vite/Vue plugins one at a time to isolate which transform removes the script-setup tag.","Clear Vite's cache (`node_modules/.vite`) and restart the dev server to rule out a stale transform cache."],"exampleFix":"// before: a custom plugin transforming md and dropping the script tag\ntransform(code, id) {\n  if (id.endsWith('.md')) return rewriteHtmlOnly(code) // drops <script setup>\n}\n// after: bail out for slide source ids so Slidev's pipeline owns them\nimport { regexSlideSourceId } from '@slidev/node/vite/common'\ntransform(code, id) {\n  if (regexSlideSourceId.test(id)) return // let Slidev handle slide modules\n  if (id.endsWith('.md')) return rewriteHtmlOnly(code)\n}","handlingStrategy":"validation","validationCode":"// If you author custom Vite plugins, never strip the script-setup tag from\n// slide source ids. Bail out for ids Slidev owns:\nimport { regexSlideSourceId } from '@slidev/node/vite/common'\nfunction ownsSlide(id: string): boolean {\n  return regexSlideSourceId.test(id)\n}\n// in your transform: if (ownsSlide(id)) return undefined","typeGuard":"// Guard the invariant the plugin relies on, for tests/diagnostics:\nconst RE_SCRIPT_SETUP_TAG = /^<script setup.*>/m\nfunction hasScriptSetup(code: string): boolean {\n  return RE_SCRIPT_SETUP_TAG.test(code)\n}","tryCatchPattern":null,"preventionTips":["Keep all @slidev/* packages on a single locked version (use the same release tag / lockfile dedupe).","Do not write Vite transform plugins that rewrite `*.md` slide modules; let Slidev's pipeline own them.","After upgrading Slidev, clear `node_modules/.vite` to prevent stale cached transforms.","If you fork Slidev's markdown-to-SFC compiler, assert every emitted slide contains a `<script setup>` tag in your tests."],"tags":["slidev","vite-plugin","internal-error","build-pipeline","markdown"],"backgroundTag":null,"analyzedSha":"0d798ace5828966d9f77f62094d5f7a971ad98f7","analyzedAt":"2026-08-12T17:10:56.221Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}