angular/angular-cli · error · Error
The '@angular-devkit/build-angular/plugins/karma' karma plug
Error message
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.
What it means
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.
Source
Thrown at packages/angular_devkit/build_angular/src/tools/webpack/plugins/karma/karma.ts:25
*/
/* eslint-disable */
// TODO: cleanup this file, it's copied as is from Angular CLI.
import type { IncomingMessage, ServerResponse } from 'node:http';
import * as path from 'node:path';
import type { Compiler } from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import { statsErrorsToString } from '../../utils/stats';
import { logging } from '@angular-devkit/core';
import { BuildOptions } from '../../../../utils/build-options';
import { normalizeSourceMaps } from '../../../../utils/index';
let webpackMiddleware: webpackDevMiddleware.API<IncomingMessage, ServerResponse>;
const init: any = (config: any, emitter: any) => {
if (!config.buildWebpack) {
throw new Error(
`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.`,
);
}
const options = config.buildWebpack.options as BuildOptions;
const logger: logging.Logger = config.buildWebpack.logger;
// Add a reporter that fixes sourcemap urls.
if (normalizeSourceMaps(options.sourceMap).scripts) {
config.reporters.unshift('@angular-devkit/build-angular--sourcemap-reporter');
// Code taken from https://github.com/tschaub/karma-source-map-support.
// We can't use it directly because we need to add it conditionally in this file, and karma
// frameworks cannot be added dynamically.
const smsPath = path.dirname(require.resolve('source-map-support'));
const ksmsPath = path.dirname(require.resolve('karma-source-map-support'));
config.files.unshift(View on GitHub (pinned to bb72145f9a)
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
Example fix
// before karma start karma.conf.js // after ng test
Defensive patterns
Strategy: validation
Validate before calling
// karma.conf.js
if (!config.buildWebpack) {
throw new Error('Run via `ng test`; the build-angular karma plugin requires the CLI builder.');
} Type guard
function isCliDriven(config: any): config is { buildWebpack: { options: unknown; logger: unknown } } {
return !!config && typeof config === 'object' && !!config.buildWebpack;
} Try / catch
try {
init(config, emitter);
} catch (err) {
if (String(err.message).includes('meant to be used from within Angular CLI')) {
console.error('Use `ng test` instead of invoking karma directly.');
process.exit(1);
}
throw err;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not find ${level} workspace.
- Invalid config found at ${workspace.filePath}. CLI should be
- Could not find a ${level} workspace. Are you in a project?
- Could not find the '${builderConf}' builder's node package.
- Cannot determine project for command. This is a multi-projec
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/474490fde6fb4068.
Report an issue: GitHub.