diff --git a/.changeset/four-moles-burn.md b/.changeset/four-moles-burn.md new file mode 100644 index 000000000000..3f45f452d914 --- /dev/null +++ b/.changeset/four-moles-burn.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Adds a helpful error for static sites when you use the `astro preview` command if you have not previously run `astro build`. diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index d5faf8cf27f3..5d6adc0c64bb 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -1,3 +1,4 @@ +import fs from 'node:fs'; import { createRequire } from 'node:module'; import { fileURLToPath, pathToFileURL } from 'node:url'; import type { AstroInlineConfig, PreviewModule, PreviewServer } from '../../@types/astro.js'; @@ -34,6 +35,12 @@ export default async function preview(inlineConfig: AstroInlineConfig): Promise< await runHookConfigDone({ settings: settings, logger: logger }); if (settings.config.output === 'static') { + if (!fs.existsSync(settings.config.outDir)) { + const outDirPath = fileURLToPath(settings.config.outDir); + throw new Error( + `[preview] The output directory ${outDirPath} does not exist. Did you run \`astro build\`?` + ); + } const server = await createStaticPreviewServer(settings, logger); return server; }