{"id":"69b57a924b474f6c","repo":"vitejs/vite","slug":"cannot-print-server-urls-before-server-listen-is-c","errorCode":null,"errorMessage":"Cannot print server URLs before server.listen is called.","messagePattern":"Cannot print server URLs before server\\.listen is called\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/server/index.ts","lineNumber":800,"sourceCode":"      }\n    },\n    async close() {\n      if (!closeServerPromise) {\n        closeServerPromise = closeServer()\n      }\n      return closeServerPromise\n    },\n    printUrls() {\n      if (server.resolvedUrls) {\n        printServerUrls(\n          server.resolvedUrls,\n          serverConfig.host,\n          config.logger.info,\n        )\n      } else if (middlewareMode) {\n        throw new Error('Cannot print server URLs in middleware mode.')\n      } else {\n        throw new Error(\n          'Cannot print server URLs before server.listen is called.',\n        )\n      }\n    },\n    bindCLIShortcuts(options) {\n      bindCLIShortcuts(server, options)\n    },\n    async restart(forceOptimize?: boolean) {\n      if (!server._restartPromise) {\n        server._forceOptimizeOnRestart = !!forceOptimize\n        server._restartPromise = restartServer(server).finally(() => {\n          server._restartPromise = null\n          server._forceOptimizeOnRestart = false\n        })\n      }\n      return server._restartPromise\n    },\n","sourceCodeStart":782,"sourceCodeEnd":818,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/server/index.ts#L782-L818","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst server = await createServer({})\nserver.printUrls() // throws\n\n// after\nconst server = await createServer({})\nawait server.listen()\nserver.printUrls()","handlingStrategy":"validation","validationCode":"function assertServerListened(server: { resolvedUrls: unknown }) {\n  if (!server.resolvedUrls) {\n    throw new Error('Await server.listen() before calling printUrls()')\n  }\n}","typeGuard":"function isServerListened(server: { resolvedUrls: unknown }): boolean {\n  return server.resolvedUrls != null\n}","tryCatchPattern":null,"preventionTips":["Await server.listen() before printUrls().","Use resolvedUrls presence as the listening signal.","In tests, assert on httpServer.address() rather than printUrls."],"tags":["dev-server","lifecycle","printurls","listen"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}