{"record":{"id":"10c1a3323fb5f481","repo":"vitejs/vite","slug":"cannot-print-server-urls-before-server-is-listenin","errorCode":null,"errorMessage":"Cannot print server URLs before server is listening.","messagePattern":"Cannot print server URLs before server is listening\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/preview.ts","lineNumber":191,"sourceCode":"    server.resolvedUrls = null\n  }\n\n  const server: PreviewServer = {\n    config,\n    middlewares: app,\n    httpServer,\n    async close() {\n      if (!closeServerPromise) {\n        closeServerPromise = closeServer()\n      }\n      return closeServerPromise\n    },\n    resolvedUrls: null,\n    printUrls() {\n      if (server.resolvedUrls) {\n        printServerUrls(server.resolvedUrls, options.host, logger.info)\n      } else {\n        throw new Error('Cannot print server URLs before server is listening.')\n      }\n    },\n    bindCLIShortcuts(options) {\n      bindCLIShortcuts(server as PreviewServer, options)\n    },\n  }\n\n  const closeServerAndExit = async (_: unknown, exitCode?: number) => {\n    try {\n      await server.close()\n    } finally {\n      process.exitCode ??= exitCode ? 128 + exitCode : undefined\n      process.exit()\n    }\n  }\n\n  setupSIGTERMListener(closeServerAndExit)\n","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/preview.ts#L173-L209","documentation":"PreviewServer.printUrls reads server.resolvedUrls, which is populated only after the HTTP server is listening. Calling printUrls before listen resolves leaves resolvedUrls null and throws. It is a programmer error in sequencing the preview server lifecycle.","triggerScenarios":"Calling server.printUrls() synchronously right after createServer (preview) without awaiting server.listen(). Or calling it inside a callback that can fire before the listen promise resolves.","commonSituations":"Programmatic wrappers around Vite preview that rush printUrls, or refactors that reordered the listen/print order.","solutions":["Await server.listen() before calling server.printUrls().","Use the printUrls callback option of listen if available, so it runs exactly when listening completes.","Guard the call: if (server.resolvedUrls) server.printUrls().","Reorder your bootstrap so the listen promise settles first."],"exampleFix":"// before\nconst server = await preview({})\nserver.printUrls()\nawait server.listen()\n// after\nconst server = await preview({})\nawait server.listen()\nserver.printUrls()","handlingStrategy":"validation","validationCode":"function safePrintUrls(server) {\n  if (!server.resolvedUrls) throw new Error('Call server.printUrls() after server.listen() resolves')\n  server.printUrls()\n}","typeGuard":"function isListening(server): boolean {\n  return server.resolvedUrls !== null\n}","tryCatchPattern":null,"preventionTips":["Always await server.listen() before printing URLs.","Use the listen callback hook if available.","Guard printUrls with a resolvedUrls null-check."],"tags":["preview","server","lifecycle","api-misuse"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}