{"record":{"id":"474490fde6fb4068","repo":"angular/angular-cli","slug":"the-angular-devkit-build-angular-plugins-karma","errorCode":null,"errorMessage":"The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to be used from within Angular CLI and will not work correctly outside of it.","messagePattern":"The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to be used from within Angular CLI and will not work correctly outside of it\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/build_angular/src/tools/webpack/plugins/karma/karma.ts","lineNumber":25,"sourceCode":" */\n\n/* eslint-disable */\n// TODO: cleanup this file, it's copied as is from Angular CLI.\nimport type { IncomingMessage, ServerResponse } from 'node:http';\nimport * as path from 'node:path';\nimport type { Compiler } from 'webpack';\nimport webpackDevMiddleware from 'webpack-dev-middleware';\n\nimport { statsErrorsToString } from '../../utils/stats';\nimport { logging } from '@angular-devkit/core';\nimport { BuildOptions } from '../../../../utils/build-options';\nimport { normalizeSourceMaps } from '../../../../utils/index';\n\nlet webpackMiddleware: webpackDevMiddleware.API<IncomingMessage, ServerResponse>;\n\nconst init: any = (config: any, emitter: any) => {\n  if (!config.buildWebpack) {\n    throw new Error(\n      `The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to` +\n        ` be used from within Angular CLI and will not work correctly outside of it.`,\n    );\n  }\n  const options = config.buildWebpack.options as BuildOptions;\n  const logger: logging.Logger = config.buildWebpack.logger;\n\n  // Add a reporter that fixes sourcemap urls.\n  if (normalizeSourceMaps(options.sourceMap).scripts) {\n    config.reporters.unshift('@angular-devkit/build-angular--sourcemap-reporter');\n\n    // Code taken from https://github.com/tschaub/karma-source-map-support.\n    // We can't use it directly because we need to add it conditionally in this file, and karma\n    // frameworks cannot be added dynamically.\n    const smsPath = path.dirname(require.resolve('source-map-support'));\n    const ksmsPath = path.dirname(require.resolve('karma-source-map-support'));\n\n    config.files.unshift(","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/build_angular/src/tools/webpack/plugins/karma/karma.ts#L7-L43","documentation":"The karma plugin shipped at @angular-devkit/build-angular/plugins/karma requires `config.buildWebpack` (options + logger) which only the Angular CLI's karma builder injects. When the plugin is loaded in a plain karma.conf.js run directly by karma (not via `ng test`), that field is absent and this error is thrown. It is an intentional guard: the plugin depends on CLI-provided webpack context.","triggerScenarios":"Running `karma start karma.conf.js` directly (or via a test runner script) on a config that includes require('@angular-devkit/build-angular/plugins/karma'), so config.buildWebpack is never populated.","commonSituations":"IDE test runners (WebStorm/VS Code) launching karma directly instead of through `ng test`; CI scripts invoking karma CLI; migrating karma configs across projects where the CLI builder wiring was dropped.","solutions":["Run tests with `ng test` so the Angular CLI karma builder injects config.buildWebpack","Remove the '@angular-devkit/build-angular/plugins/karma' require from karma.conf.js if you are not using the CLI builder","If an IDE must launch tests, configure it to execute the `ng test` command (or the builder) rather than karma directly"],"exampleFix":"// before\nkarma start karma.conf.js\n// after\nng test","handlingStrategy":"validation","validationCode":"// karma.conf.js\nif (!config.buildWebpack) {\n  throw new Error('Run via `ng test`; the build-angular karma plugin requires the CLI builder.');\n}","typeGuard":"function isCliDriven(config: any): config is { buildWebpack: { options: unknown; logger: unknown } } {\n  return !!config && typeof config === 'object' && !!config.buildWebpack;\n}","tryCatchPattern":"try {\n  init(config, emitter);\n} catch (err) {\n  if (String(err.message).includes('meant to be used from within Angular CLI')) {\n    console.error('Use `ng test` instead of invoking karma directly.');\n    process.exit(1);\n  }\n  throw err;\n}","preventionTips":["Always launch tests with `ng test`","Configure IDE test runners to invoke the Angular builder","Remove the karma plugin from non-CLI karma setups","Document in CI that direct `karma start` is unsupported"],"tags":["karma","angular-cli","misconfiguration","unit-testing"],"backgroundTag":"plugin-requires-angular-cli-context","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}