angular/angular-cli · error · SchematicsException

Service worker requires a project type of "application".

Error message

Service worker requires a project type of "application".

What it means

Thrown by the service-worker schematic when the target project's projectType extension is not "application". Service workers only apply to application builds (index.html + production build output), so the schematic rejects libraries.

Source

Thrown at packages/schematics/angular/service-worker/index.ts:111

        code`${external('provideServiceWorker', '@angular/service-worker')}('ngsw-worker.js', {
            enabled: !isDevMode(),
            registrationStrategy: 'registerWhenStable:30000'
          })`,
    );
  };
}

function getTsSourceFile(host: Tree, path: string): ts.SourceFile {
  const content = host.readText(path);
  const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);

  return source;
}

const serviceWorkerSchematic: RuleFactory<ServiceWorkerOptions> = createProjectSchematic(
  async (options, { project, workspace, tree, context: { logger } }) => {
    if (project.extensions.projectType !== 'application') {
      throw new SchematicsException(`Service worker requires a project type of "application".`);
    }
    const buildTarget = project.targets.get('build');
    if (!buildTarget) {
      throw targetBuildNotFoundError();
    }

    const buildOptions = buildTarget.options as Record<string, string | boolean>;
    const browserEntryPoint = await getMainFilePath(tree, options.project);
    const ngswConfigPath = join(project.root, 'ngsw-config.json');

    if (
      buildTarget.builder === Builders.Application ||
      buildTarget.builder === Builders.BuildApplication
    ) {
      const productionConf = buildTarget.configurations?.production;
      if (productionConf) {
        productionConf.serviceWorker = ngswConfigPath;
      } else {

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run the schematic against an application: `ng g @angular/pwa --project <app-name>`.
  2. Set "projectType": "application" in angular.json if the project truly is an application but misconfigured.
  3. Do not add a service worker to libraries; configure PWA at the application level.
  4. Confirm targets with `ng list` before running.

Example fix

// before
ng g @angular/pwa --project ui-lib
// after
ng g @angular/pwa --project storefront
Defensive patterns

Strategy: validation

Validate before calling

const ws = JSON.parse(fs.readFileSync('angular.json', 'utf8'));
const p = ws.projects?.[projectName];
if (p?.projectType !== 'application') {
  throw new Error(`Service worker requires an application project; '${projectName}' is not one.`);
}

Type guard

function isApplicationProject(p: { projectType?: string } | undefined): p is { projectType: 'application' } {
  return p?.projectType === 'application';
}

Try / catch

try {
  await generateServiceWorkerSchematic({ project: projectName });
} catch (e) {
  if (String(e.message).includes('project type of "application"')) {
    console.error(`Pass an application via --project; '${projectName}' is a library.`);
  } else throw e;
}

Prevention

When it happens

Trigger: Running `ng g @angular/pwa --project <lib>` (or the serviceWorkerSchematic Rule) against a project whose project.extensions.projectType is 'library' or undefined.

Common situations: Attempting to add PWA/service-worker to a library; custom projects in angular.json missing the projectType extension; wrong --project name in a multi-project workspace selecting a library.

Related errors


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