angular/angular-cli · error · SchematicsException

Could not find package.json

Error message

Could not find package.json

What it means

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.

Source

Thrown at packages/schematics/angular/ssr/index.ts:121

  if (typeof outputPath !== 'string') {
    throw new SchematicsException(
      `outputPath for ${projectName} ${target} target is not a string.`,
    );
  }

  return {
    base: outputPath,
    ...defaultDirs,
  };
}

function addScriptsRule({ project }: SSROptions, isUsingApplicationBuilder: boolean): Rule {
  return async (host) => {
    const pkgPath = '/package.json';
    const pkg = host.readJson(pkgPath) as { scripts?: Record<string, string> } | null;
    if (pkg === null) {
      throw new SchematicsException('Could not find package.json');
    }

    if (isUsingApplicationBuilder) {
      const { base, server } = await getApplicationBuilderOutputPaths(host, project);
      pkg.scripts ??= {};
      pkg.scripts[`serve:ssr:${project}`] = `node ${join(base, server)}/server.mjs`;
    } else {
      const serverDist = await getLegacyOutputPaths(host, project, 'server');
      pkg.scripts = {
        ...pkg.scripts,
        'dev:ssr': `ng run ${project}:${SERVE_SSR_TARGET_NAME}`,
        'serve:ssr': `node ${serverDist}/main.js`,
        'build:ssr': `ng build && ng run ${project}:server`,
        'prerender': `ng run ${project}:${PRERENDER_TARGET_NAME}`,
      };
    }

    host.overwrite(pkgPath, JSON.stringify(pkg, null, 2));

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run the schematic from the workspace root where package.json exists (`ng add @angular/ssr --project <name>` executed in the root directory).
  2. Restore or recreate package.json (`npm init -y`) before running the schematic, then re-run.
  3. In monorepos, ensure the Angular workspace root is the directory containing package.json and angular.json together.

Example fix

// before
projects/app/           # running ng add from here, no package.json in tree root
// after
cd workspace-root       # contains package.json and angular.json
ng add @angular/ssr --project app
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync } from 'fs';
if (!existsSync('package.json')) {
  throw new Error('Run the SSR schematic from the directory containing package.json.');
}

Type guard

null

Try / catch

try {
  await ngAdd('@angular/ssr');
} catch (e) {
  if (String(e?.message).includes('Could not find package.json')) {
    console.error('Run from the workspace root where package.json and angular.json live.');
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/eb8f6ad521863cb0. Report an issue: GitHub.