{"record":{"id":"69cba91f46b82d8e","repo":"vitejs/vite","slug":"there-is-already-a-server-associated-with-the-conf","errorCode":null,"errorMessage":"There is already a server associated with the config.","messagePattern":"There is already a server associated with the config\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/server/index.ts","lineNumber":505,"sourceCode":"  inlineConfig: ResolvedConfig | InlineConfig | undefined = {},\n  options: {\n    listen: boolean\n    previousEnvironments?: Record<string, DevEnvironment>\n    previousShortcutsState?: ShortcutsState<ViteDevServer>\n    previousRestartPromise?: Promise<void> | null\n    previousForceOptimizeOnRestart?: boolean\n  },\n): Promise<ViteDevServer> {\n  // The dev server is a long-running, interactive process whose outputs\n  // (network responses, HMR updates) cannot be replayed from a cache.\n  disableCache()\n\n  const config = isResolvedConfig(inlineConfig)\n    ? inlineConfig\n    : await resolveConfig(inlineConfig, 'serve')\n\n  if (usedConfigs.has(config)) {\n    throw new Error(`There is already a server associated with the config.`)\n  }\n\n  if (config.command !== 'serve') {\n    throw new Error(\n      `Config was resolved for a \"build\", expected a \"serve\" command.`,\n    )\n  }\n\n  usedConfigs.add(config)\n\n  const initPublicFilesPromise = initPublicFiles(config)\n\n  const { root, server: serverConfig } = config\n  const httpsOptions = await resolveHttpsConfig(config.server.https)\n  const { middlewareMode } = serverConfig\n\n  const resolvedOutDirs = getResolvedOutDirs(\n    config.root,","sourceCodeStart":487,"sourceCodeEnd":523,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/server/index.ts#L487-L523","documentation":"Thrown by Vite's dev server factory when the ResolvedConfig object passed to createServer() is already registered in the internal usedConfigs WeakSet. Vite binds one dev server per resolved config to avoid duplicate servers, port conflicts, and shared mutable state (plugins, module graph) going out of sync.","triggerScenarios":"Calling createServer(resolvedConfig) twice with the exact same ResolvedConfig instance returned by resolveConfig(...). Reusing a cached config across hot paths or test setup/teardown without creating a fresh one.","commonSituations":"Test suites that resolve config once for speed then spawn multiple servers. Framework integrations that cache resolveConfig output and call createServer on restart. Sharing the same config object between a build watcher and a dev server.","solutions":["Create a new ResolvedConfig per server by calling createServer(inlineConfig) with the raw inline config each time, letting Vite resolve internally.","If you hold a ResolvedConfig, close the existing server (await server.close()) before starting a new one with the same config.","For tests, resolve config inside beforeEach, not in a top-level shared variable."],"exampleFix":"// before\nconst config = await resolveConfig(cfg, 'serve')\nconst s1 = await createServer(config)\nconst s2 = await createServer(config) // throws\n// after\nconst s1 = await createServer(cfg)\nawait s1.close()\nconst s2 = await createServer(cfg)","handlingStrategy":"validation","validationCode":"// don't reuse ResolvedConfig across servers; pass InlineConfig each time\nconst server = await createServer(inlineConfig)","typeGuard":null,"tryCatchPattern":"try {\n  await createServer(config)\n} catch (e) {\n  if (String(e?.message).includes('already a server')) {\n    await prevServer.close()\n    return createServer(inlineConfig) // fresh resolve\n  }\n  throw e\n}","preventionTips":["Never share one ResolvedConfig between two createServer calls; close the old server first.","In tests, resolve config per-test, not in a shared module-level constant."],"tags":["dev-server","configuration","lifecycle"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}