{"record":{"id":"6b9ea2886b3faf69","repo":"modelcontextprotocol/servers","slug":"could-not-locate-package-json-for-server-version-6b9ea2","errorCode":null,"errorMessage":"Could not locate package.json for server version","messagePattern":"Could not locate package\\.json for server version","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/memory/version.ts","lineNumber":24,"sourceCode":"  const require = createRequire(import.meta.url);\n  const moduleDir = path.dirname(fileURLToPath(import.meta.url));\n  const candidates = [\n    path.join(moduleDir, 'package.json'),\n    path.join(moduleDir, '..', 'package.json'),\n  ];\n\n  for (const candidate of candidates) {\n    try {\n      const pkg = require(candidate) as { version?: string };\n      if (pkg.version) {\n        return pkg.version;\n      }\n    } catch {\n      // Try the next candidate when running from dist/ or source.\n    }\n  }\n\n  throw new Error('Could not locate package.json for server version');\n}\n\nexport const SERVER_VERSION = resolvePackageVersion();\n","sourceCodeStart":6,"sourceCodeEnd":28,"githubUrl":"https://github.com/modelcontextprotocol/servers/blob/d73f99efbfd40c3aa1b61e88728b3d49fb52608f/src/memory/version.ts#L6-L28","documentation":"resolvePackageVersion() walks candidate package.json locations relative to the compiled module (moduleDir and its parent) and throws when none can be read or none contains a version field. Because SERVER_VERSION is computed at module load time, this error crashes the server at startup whenever the package layout does not include a package.json next to version.ts/dist.","triggerScenarios":"Running the built dist/index.js in isolation (copied out of node_modules or a container) without package.json beside it; bundling with a packer (esbuild/webpack/ncc) that does not emit package.json into the output; running from a source checkout after build layout changes so neither dist/../package.json nor dist/package.json resolves.","commonSituations":"Docker images that COPY only dist/ and not package.json; npm pack/tgz installs pruned unexpectedly; monorepo symlinked builds where the candidate paths point at the wrong directory; bundled single-file deployments.","solutions":["Ensure package.json is shipped/copied alongside the built output (e.g. COPY src/memory/package.json next to dist/ in Docker).","If bundling, mark the module external or emit package.json into the bundle output directory.","Build with the package's own build script (npm run build) instead of ad-hoc tsc invocations that change the output layout.","As a workaround, set the version through the environment or patch resolvePackageVersion to fall back to a constant when the file is absent."],"exampleFix":"// before (Dockerfile)\nCOPY dist/ ./dist/\nCMD [\"node\", \"dist/index.js\"]\n// after\nCOPY dist/ ./dist/\nCOPY package.json ./dist/../package.json\nCMD [\"node\", \"dist/index.js\"]","handlingStrategy":"fallback","validationCode":"import { existsSync } from \"fs\";\nimport path from \"path\";\nimport { fileURLToPath } from \"url\";\nconst dir = path.dirname(fileURLToPath(import.meta.url));\nif (!existsSync(path.join(dir, \"package.json\")) && !existsSync(path.join(dir, \"..\", \"package.json\"))) {\n  console.warn(\"package.json not found next to build output; SERVER_VERSION will throw\");\n}","typeGuard":null,"tryCatchPattern":"// The throw happens at import time (SERVER_VERSION), so wrap the dynamic import instead:\nlet serverVersion = \"unknown\";\ntry {\n  ({ SERVER_VERSION } = await import(\"./version.js\"));\n  serverVersion = SERVER_VERSION;\n} catch (e) {\n  console.error(\"Version resolution failed; check package.json is shipped with dist/\", e);\n}","preventionTips":["In Docker/bundles, always copy package.json alongside dist/ output.","Smoke-test the built artifact in a clean directory before publishing.","Prefer running via the package's bin entry (node_modules layout intact) rather than copying index.js out."],"tags":["startup","package-json","version-resolution","typescript"],"backgroundTag":"config-file-not-found","analyzedSha":"d73f99efbfd40c3aa1b61e88728b3d49fb52608f","analyzedAt":"2026-09-07T14:54:21.545Z","contentChangedAt":"2026-09-07T14:54:21.545Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}