vitejs/vite · error · Error
Cannot print server URLs before server.listen is called.
Error message
Cannot print server URLs before server.listen is called.
What it means
ViteDevServer.printUrls at packages/vite/src/node/server/index.ts:800 throws when resolvedUrls is still null in non-middleware mode. resolvedUrls is filled only after the http server actually listens (the listening callback resolves host/port/urls), so calling printUrls before server.listen() cannot print anything meaningful.
Source
Thrown at packages/vite/src/node/server/index.ts:800
}
},
async close() {
if (!closeServerPromise) {
closeServerPromise = closeServer()
}
return closeServerPromise
},
printUrls() {
if (server.resolvedUrls) {
printServerUrls(
server.resolvedUrls,
serverConfig.host,
config.logger.info,
)
} else if (middlewareMode) {
throw new Error('Cannot print server URLs in middleware mode.')
} else {
throw new Error(
'Cannot print server URLs before server.listen is called.',
)
}
},
bindCLIShortcuts(options) {
bindCLIShortcuts(server, options)
},
async restart(forceOptimize?: boolean) {
if (!server._restartPromise) {
server._forceOptimizeOnRestart = !!forceOptimize
server._restartPromise = restartServer(server).finally(() => {
server._restartPromise = null
server._forceOptimizeOnRestart = false
})
}
return server._restartPromise
},
View on GitHub (pinned to 89620f09af)
Solutions
- Await server.listen() (or call printUrls inside its callback) so resolvedUrls is populated.
- Move printUrls() to the point where you know the server is listening.
- Omit the call entirely if Vite's default listen already prints URLs for you.
Example fix
// before
const server = await createServer({})
server.printUrls() // throws
// after
const server = await createServer({})
await server.listen()
server.printUrls() Defensive patterns
Strategy: validation
Validate before calling
function assertServerListened(server: { resolvedUrls: unknown }) {
if (!server.resolvedUrls) {
throw new Error('Await server.listen() before calling printUrls()')
}
} Type guard
function isServerListened(server: { resolvedUrls: unknown }): boolean {
return server.resolvedUrls != null
} Prevention
- Await server.listen() before printUrls().
- Use resolvedUrls presence as the listening signal.
- In tests, assert on httpServer.address() rather than printUrls.
When it happens
Trigger: Calling server.printUrls() synchronously right after createServer without awaiting server.listen(); calling printUrls inside an error handler when listen failed; test fixtures that create the server and immediately print.
Common situations: Custom dev scripts that forget the listen step; race conditions where printUrls runs before the listen promise resolves; refactoring that moved listen into an async branch but left printUrls at the top.
Related errors
- Cannot print server URLs before server is listening.
- There is already a server associated with the config.
- Cannot print server URLs in middleware mode.
- Vite module runner has been closed.
- [module runner] HMR client was closed.
AI-assisted analysis of vitejs/vite@89620f09af (2026-08-03).
Data as JSON: /data/errors/69b57a924b474f6c.json.
Report an issue: GitHub.