diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 3a5cbc0c8e566c..7dfa8851debf06 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' @@ -83,6 +84,13 @@ export async function preview( '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, @@ -115,7 +123,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 const assetServer = sirv(distDir, { etag: true,