{"record":{"id":"055b4e9d4c1faa1e","repo":"angular/angular-cli","slug":"service-worker-requires-a-project-type-of-applica","errorCode":null,"errorMessage":"Service worker requires a project type of \"application\".","messagePattern":"Service worker requires a project type of \"application\"\\.","errorType":"exception","errorClass":"SchematicsException","httpStatus":null,"severity":"error","filePath":"packages/schematics/angular/service-worker/index.ts","lineNumber":111,"sourceCode":"        code`${external('provideServiceWorker', '@angular/service-worker')}('ngsw-worker.js', {\n            enabled: !isDevMode(),\n            registrationStrategy: 'registerWhenStable:30000'\n          })`,\n    );\n  };\n}\n\nfunction getTsSourceFile(host: Tree, path: string): ts.SourceFile {\n  const content = host.readText(path);\n  const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);\n\n  return source;\n}\n\nconst serviceWorkerSchematic: RuleFactory<ServiceWorkerOptions> = createProjectSchematic(\n  async (options, { project, workspace, tree, context: { logger } }) => {\n    if (project.extensions.projectType !== 'application') {\n      throw new SchematicsException(`Service worker requires a project type of \"application\".`);\n    }\n    const buildTarget = project.targets.get('build');\n    if (!buildTarget) {\n      throw targetBuildNotFoundError();\n    }\n\n    const buildOptions = buildTarget.options as Record<string, string | boolean>;\n    const browserEntryPoint = await getMainFilePath(tree, options.project);\n    const ngswConfigPath = join(project.root, 'ngsw-config.json');\n\n    if (\n      buildTarget.builder === Builders.Application ||\n      buildTarget.builder === Builders.BuildApplication\n    ) {\n      const productionConf = buildTarget.configurations?.production;\n      if (productionConf) {\n        productionConf.serviceWorker = ngswConfigPath;\n      } else {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/schematics/angular/service-worker/index.ts#L93-L129","documentation":"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.","triggerScenarios":"Running `ng g @angular/pwa --project <lib>` (or the serviceWorkerSchematic Rule) against a project whose project.extensions.projectType is 'library' or undefined.","commonSituations":"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.","solutions":["Run the schematic against an application: `ng g @angular/pwa --project <app-name>`.","Set \"projectType\": \"application\" in angular.json if the project truly is an application but misconfigured.","Do not add a service worker to libraries; configure PWA at the application level.","Confirm targets with `ng list` before running."],"exampleFix":"// before\nng g @angular/pwa --project ui-lib\n// after\nng g @angular/pwa --project storefront","handlingStrategy":"validation","validationCode":"const ws = JSON.parse(fs.readFileSync('angular.json', 'utf8'));\nconst p = ws.projects?.[projectName];\nif (p?.projectType !== 'application') {\n  throw new Error(`Service worker requires an application project; '${projectName}' is not one.`);\n}","typeGuard":"function isApplicationProject(p: { projectType?: string } | undefined): p is { projectType: 'application' } {\n  return p?.projectType === 'application';\n}","tryCatchPattern":"try {\n  await generateServiceWorkerSchematic({ project: projectName });\n} catch (e) {\n  if (String(e.message).includes('project type of \"application\"')) {\n    console.error(`Pass an application via --project; '${projectName}' is a library.`);\n  } else throw e;\n}","preventionTips":["Add PWA/service-worker only to application projects","Confirm projectType before running project-scoped schematics","Avoid typos in --project in multi-project workspaces","Keep custom angular.json entries with correct projectType"],"tags":["angular","schematics","service-worker","project-type"],"backgroundTag":"wrong-project-type","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}