angular/angular-cli · error · CommandModuleError

Invalid JSON path.

Error message

Invalid JSON path.

What it means

parseJsonPath() converts a dot/bracket JSON path string (e.g. schematics['@scope/pkg'].style) into an array of keys/indices. Each fragment must match the regex /([^[]+)((\[.*\])*)/; if a fragment does not, the path is malformed and a CommandModuleError('Invalid JSON path.') is thrown.

Source

Thrown at packages/angular/cli/src/commands/config/cli.ts:150

 * by the path. For example, a path of "a[3].foo.bar[2]" would give you a fragment array of
 * ["a", 3, "foo", "bar", 2].
 * @param path The JSON string to parse.
 * @returns {(string|number)[]} The fragments for the string.
 * @private
 */
function parseJsonPath(path: string): (string | number)[] {
  const fragments = (path || '').split(/\./g);
  const result: (string | number)[] = [];

  while (fragments.length > 0) {
    const fragment = fragments.shift();
    if (fragment == undefined) {
      break;
    }

    const match = fragment.match(/([^[]+)((\[.*\])*)/);
    if (!match) {
      throw new CommandModuleError('Invalid JSON path.');
    }

    result.push(match[1]);
    if (match[2]) {
      const indices = match[2]
        .slice(1, -1)
        .split('][')
        .map((x) => (/^\d$/.test(x) ? +x : x.replace(/"|'/g, '')));
      result.push(...indices);
    }
  }

  return result.filter((fragment) => fragment != null);
}

function normalizeValue(value: string | undefined | boolean | number): JsonValue | undefined {
  const valueString = `${value}`.trim();
  switch (valueString) {

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Use dot notation rooted at a key: `ng config cli.defaultCollection value` instead of `["cli"].defaultCollection`
  2. For scoped schematic packages, keep a non-bracket prefix: `ng config schematics.@angular/core.component.style scss`
  3. Remove leading/trailing dots or brackets from the path
  4. Verify the final argument after shell quoting is a plain path string (print it with echo)

Example fix

// before
ng config "[cli].defaultCollection" my-lib
// after
ng config cli.defaultCollection my-lib
Defensive patterns

Strategy: validation

Validate before calling

// Mirror of the parser's expectation: fragment must have non-bracket chars and be dot/bracket separated
function isParseableJsonPath(p: string): boolean {
  return p.split('.')
    .filter(Boolean)
    .every((f) => /([^[]+)((\[.*\])*)/.test(f));
}
if (!isParseableJsonPath(jsonPath)) {
  throw new Error(`Unsupported JSON path: ${jsonPath}. Use key.subKey or key["subKey"] form starting with a key name.`);
}

Type guard

function isDotPrefixedPath(p: string): p is string {
  return /^[A-Za-z0-9_@\-]/.test(p); // must start with a key name, not '[' or '.'
}

Try / catch

try {
  await ngConfigSet(jsonPath, value);
} catch (e) {
  if (e instanceof CommandModuleError && e.message === 'Invalid JSON path.') {
    logger.error(`Cannot parse '${jsonPath}'. Use forms like cli.analytics or schematics["@scope/pkg"].style.`);
  } else { throw e; }
}

Prevention

When it happens

Trigger: Supplying a jsonPath to `ng config` whose fragment has no non-bracket characters or otherwise fails the regex — e.g. a path starting with '[' (like "[0].name"), consecutive/empty bracket segments in a way the regex can't capture, or paths containing only brackets such as "[\"cli\"]".

Common situations: Passing array-index-first paths; quoting mistakes that leave empty fragments; using JSON-pointer style paths (/cli/x) or leading dots (.cli.x) that the parser does not accept.

Understand the failure class

Related errors


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