angular/angular-cli · error

Invalid package name

Error message

Invalid package name

What it means

After loading the package.json, addBuilderFromPackage() reads packageJson.name to register builders under it. If the field is missing or empty, the host throws 'Invalid package name'.

Source

Thrown at packages/angular_devkit/architect/testing/testing-architect-host.ts:46

  ) {}

  addBuilder(
    builderName: string,
    builder: Builder,
    description = 'Testing only builder.',
    optionSchema: json.schema.JsonSchema = { type: 'object' },
  ): void {
    this._builderImportMap.set(builderName, builder);
    this._builderMap.set(builderName, { builderName, description, optionSchema });
  }
  async addBuilderFromPackage(packageName: string): Promise<void> {
    const packageJson = await import(packageName + '/package.json');
    if (!('builders' in packageJson)) {
      throw new Error('Invalid package.json, builders key not found.');
    }

    if (!packageJson.name) {
      throw new Error('Invalid package name');
    }

    const builderJsonPath = packageName + '/' + packageJson['builders'];
    const builderJson = await import(builderJsonPath);
    const builders = builderJson['builders'];
    if (!builders) {
      throw new Error('Invalid builders.json, builders key not found.');
    }

    for (const builderName of Object.keys(builders)) {
      const b = builders[builderName];
      // TODO: remove this check as v1 is not supported anymore.
      if (!b.implementation) {
        continue;
      }
      const handler = (await import(builderJsonPath + '/../' + b.implementation)).default;
      const optionsSchema = await import(builderJsonPath + '/../' + b.schema);
      this.addBuilder(`${packageJson.name}:${builderName}`, handler, b.description, optionsSchema);

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Add a "name" field to the package's package.json.
  2. Make sure you are importing the intended package, not a stub fixture missing metadata.
  3. If the package is yours, ensure npm metadata wasn't stripped during packaging.

Example fix

// before (package.json)
{ "builders": "./builders.json" }
// after
{ "name": "@myorg/builders", "builders": "./builders.json" }
Defensive patterns

Strategy: validation

Validate before calling

const pkg = require(packageName + '/package.json');
if (!pkg.name) {
  throw new Error(`${packageName}'s package.json is missing a name field`);
}
await architectHost.addBuilderFromPackage(packageName);

Type guard

function hasValidName(pkg: object): pkg is { name: string } {
  return typeof (pkg as any).name === 'string' && (pkg as any).name.length > 0;
}

Try / catch

try {
  await architectHost.addBuilderFromPackage(packageName);
} catch (e) {
  if (e.message === 'Invalid package name') {
    throw new Error(`Fix package.json of ${packageName}: missing "name"`);
  }
  throw e;
}

Prevention

When it happens

Trigger: addBuilderFromPackage(packageName) resolving to a package.json that lacks a non-empty 'name' field.

Common situations: Hand-crafted or generated test fixtures where package.json was created without a name; minimally scaffolded packages in temp directories used in tests.

Related errors


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