{"record":{"id":"eb8f6ad521863cb0","repo":"angular/angular-cli","slug":"could-not-find-package-json","errorCode":null,"errorMessage":"Could not find package.json","messagePattern":"Could not find package\\.json","errorType":"exception","errorClass":"SchematicsException","httpStatus":null,"severity":"error","filePath":"packages/schematics/angular/ssr/index.ts","lineNumber":121,"sourceCode":"\n  if (typeof outputPath !== 'string') {\n    throw new SchematicsException(\n      `outputPath for ${projectName} ${target} target is not a string.`,\n    );\n  }\n\n  return {\n    base: outputPath,\n    ...defaultDirs,\n  };\n}\n\nfunction addScriptsRule({ project }: SSROptions, isUsingApplicationBuilder: boolean): Rule {\n  return async (host) => {\n    const pkgPath = '/package.json';\n    const pkg = host.readJson(pkgPath) as { scripts?: Record<string, string> } | null;\n    if (pkg === null) {\n      throw new SchematicsException('Could not find package.json');\n    }\n\n    if (isUsingApplicationBuilder) {\n      const { base, server } = await getApplicationBuilderOutputPaths(host, project);\n      pkg.scripts ??= {};\n      pkg.scripts[`serve:ssr:${project}`] = `node ${join(base, server)}/server.mjs`;\n    } else {\n      const serverDist = await getLegacyOutputPaths(host, project, 'server');\n      pkg.scripts = {\n        ...pkg.scripts,\n        'dev:ssr': `ng run ${project}:${SERVE_SSR_TARGET_NAME}`,\n        'serve:ssr': `node ${serverDist}/main.js`,\n        'build:ssr': `ng build && ng run ${project}:server`,\n        'prerender': `ng run ${project}:${PRERENDER_TARGET_NAME}`,\n      };\n    }\n\n    host.overwrite(pkgPath, JSON.stringify(pkg, null, 2));","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/schematics/angular/ssr/index.ts#L103-L139","documentation":"The SSR schematic's addScriptsRule reads the workspace root /package.json to add serve/build scripts (e.g. `serve:ssr:<project>`). If the host tree has no package.json at the root, the schematic cannot update scripts and throws this SchematicsException.","triggerScenarios":"Running the ssr schematic in a directory where the schematic's execution tree has no /package.json — e.g. running from outside an npm workspace root, a deleted or renamed package.json, or a host tree created by tooling that omitted it.","commonSituations":"Running `ng add @angular/ssr` in a project folder that is not the workspace root; package.json deleted after a failed migration; non-standard monorepo layouts where package.json sits above the tree root visible to the schematic.","solutions":["Run the schematic from the workspace root where package.json exists (`ng add @angular/ssr --project <name>` executed in the root directory).","Restore or recreate package.json (`npm init -y`) before running the schematic, then re-run.","In monorepos, ensure the Angular workspace root is the directory containing package.json and angular.json together."],"exampleFix":"// before\nprojects/app/           # running ng add from here, no package.json in tree root\n// after\ncd workspace-root       # contains package.json and angular.json\nng add @angular/ssr --project app","handlingStrategy":"validation","validationCode":"import { existsSync } from 'fs';\nif (!existsSync('package.json')) {\n  throw new Error('Run the SSR schematic from the directory containing package.json.');\n}","typeGuard":"null","tryCatchPattern":"try {\n  await ngAdd('@angular/ssr');\n} catch (e) {\n  if (String(e?.message).includes('Could not find package.json')) {\n    console.error('Run from the workspace root where package.json and angular.json live.');\n  }\n  throw e;\n}","preventionTips":["Always run ng add/ng generate from the workspace root.","Never delete package.json mid-migration; restore from git if a migration fails.","In monorepos, confirm package.json sits at the Angular workspace root alongside angular.json."],"tags":["angular","schematics","ssr","package-json","filesystem"],"backgroundTag":"package-json-not-found","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}