diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index d17fc0845326dc..9bbc70eb2cd101 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -1,3 +1,4 @@ +import fs from 'node:fs' import path from 'node:path' import type * as http from 'node:http' import sirv from 'sirv' @@ -78,6 +79,13 @@ export async function preview( ): Promise { const config = await resolveConfig(inlineConfig, 'serve', 'production') + const distDir = path.resolve(config.root, config.build.outDir) + if (!fs.existsSync(distDir)) { + throw new Error( + `"${config.build.outDir}" does not exist. Did you build your project?` + ) + } + const app = connect() as Connect.Server const httpServer = await resolveHttpServer( config.preview, @@ -110,7 +118,6 @@ export async function preview( config.base === './' || config.base === '' ? '/' : config.base // static assets - const distDir = path.resolve(config.root, config.build.outDir) const headers = config.preview.headers app.use( previewBase,