slidevjs/slidev · error · Error

Code snippet path not found: ${src}

Error message

Code snippet path not found: ${src}

What it means

Thrown by resolveSnippetImport when the snippet path passed isPathInsideRoots but does not exist on disk or is not a regular file (fs.existsSync false, or fs.statSync(...).isFile() false). Fires at parse time during markdown rendering of a <<< import line.

Source

Thrown at packages/slidev/node/syntax/snippet.ts:133

  let [, filepath = '', regionName = '', lang = '', meta = ''] = match
  const dir = path.dirname(slide.source.filepath)
  const src = slash(
    filepath.startsWith('@/')
      ? path.resolve(userRoot, filepath.slice(2))
      : path.resolve(dir, filepath),
  )

  lang = lang.trim() || path.extname(filepath).slice(1)
  meta = meta.trim()

  if (!isPathInsideRoots(src, allowedRoots)) {
    throw new Error(`Code snippet path escapes the project root: ${src}`)
  }

  const isAFile = fs.existsSync(src) && fs.statSync(src).isFile()
  if (!isAFile) {
    throw new Error(`Code snippet path not found: ${src}`)
  }

  let content = fs.readFileSync(src, 'utf8')

  if (regionName) {
    const lines = content.split(RE_NEWLINE)
    const region = findRegion(lines, regionName.slice(1))
    if (region) {
      content = dedent(
        lines
          .slice(region.start, region.end)
          .filter(l => !(region.re.start.test(l) || region.re.end.test(l)))
          .join('\n'),
      )
    }
  }

  return { content, filepath, lang, meta, src }

View on GitHub (pinned to 0d798ace58)

Solutions

  1. Verify the file exists at the resolved absolute path the error prints.
  2. Correct typos and case in the snippet path.
  3. If the file was moved, update the import to its new location.
  4. Ensure the path points to a file, not a directory.

Example fix

// before: wrong filename (typo)
<<< ./src/snippit.ts

// after: correct filename
<<< ./src/snippet.ts
Defensive patterns

Strategy: validation

Validate before calling

import fs from 'node:fs'
import path from 'pathe'

function snippetFileExists(dir: string, userRoot: string, filepath: string): boolean {
  const src = filepath.startsWith('@/')
    ? path.resolve(userRoot, filepath.slice(2))
    : path.resolve(dir, filepath)
  return fs.existsSync(src) && fs.statSync(src).isFile()
}

// before relying on a <<< import:
if (!snippetFileExists(slideDir, userRoot, filepath)) {
  throw new Error(`Snippet file missing: ${filepath}`)
}

Type guard

function snippetFileResolves(dir: string, userRoot: string, filepath: string): boolean {
  const src = filepath.startsWith('@/')
    ? path.resolve(userRoot, filepath.slice(2))
    : path.resolve(dir, filepath)
  try {
    return fs.existsSync(src) && fs.statSync(src).isFile()
  } catch {
    return false
  }
}

Try / catch

try {
  return resolveSnippetImport(line, userRoot, slide, allowedRoots)
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Code snippet path not found')) {
    // surface the resolved path; offer to create or fix the import
    return null
  }
  throw e
}

Prevention

When it happens

Trigger: Writing <<< ./missing.ts or <<< @/typo/file.js where the target file does not exist, or where the path resolves to a directory rather than a file.

Common situations: Typo in the snippet path; file renamed/deleted after the slide was written; case-sensitivity mismatch on case-sensitive filesystems (Linux) when authoring on macOS/Windows; pointing at a directory by mistake.

Related errors


AI-assisted analysis of slidevjs/slidev@0d798ace58 (2026-08-12). Data as JSON: /api/errors/95ea41747ed720e6. Report an issue: GitHub.