yarnpkg/yarn · error · MessageError

invalidPackageName

Error message

invalidPackageName

What it means

Before mutating package owners, the owner command resolves the package name and validates it with `isValidPackageName(name)` (owner.js:31). If the name fails npm naming rules, it throws invalidPackageName before contacting the registry.

Source

Thrown at src/cli/commands/owner.js:32

  success: string,
  error: string,
};

export async function mutate(
  args: Array<string>,
  config: Config,
  reporter: Reporter,
  buildMessages: (username: string, packageName: string) => Messages,
  mutator: (user: Object, pkg: Object) => boolean,
): Promise<boolean> {
  if (args.length !== 2 && args.length !== 1) {
    return false;
  }

  const username = args.shift();
  const name = await getName(args, config);
  if (!isValidPackageName(name)) {
    throw new MessageError(reporter.lang('invalidPackageName'));
  }

  const msgs = buildMessages(username, name);
  reporter.step(1, 3, reporter.lang('loggingIn'));
  const revoke = await getToken(config, reporter, name);

  reporter.step(2, 3, msgs.info);
  const user = await config.registries.npm.request(`-/user/org.couchdb.user:${username}`);
  let error = false;
  if (user) {
    // get package
    const pkg = await config.registries.npm.request(NpmRegistry.escapeName(name));
    if (pkg) {
      pkg.maintainers = pkg.maintainers || [];
      error = mutator({name: user.name, email: user.email}, pkg);
    } else {
      error = true;
      reporter.error(reporter.lang('unknownPackage', name));

View on GitHub (pinned to c2dda503f3)

Solutions

  1. Use the exact published package name (check it on the registry or in package.json `name`).
  2. For scoped packages pass `@scope/name` with lowercase alphanumeric, hyphens, and underscores only.
  3. Avoid uppercase, spaces, and leading punctuation in the name argument.

Example fix

// before
$ yarn owner add alice My-Lib
// after
$ yarn owner add alice my-lib
Defensive patterns

Strategy: validation

Validate before calling

// Mirror npm's package-name rules before calling owner commands
function isValidPackageName(name) {
  return /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(name);
}
if (!isValidPackageName(pkgName)) {
  throw new Error(`'${pkgName}' is not a valid npm package name.`);
}

Type guard

function isValidPackageName(name) {
  return typeof name === 'string' &&
    /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(name);
}

Try / catch

try {
  await runYarn(['owner', verb, user, pkgName]);
} catch (e) {
  if (/invalidPackageName/.test(e.message)) {
    console.error(`'${pkgName}' must be lowercase, no spaces, valid scope.`);
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Running `yarn owner add/remove <user> <pkg>` where `<pkg>` contains uppercase letters, spaces, leading dots/underscores, invalid special chars, or a malformed scope like `@scope` without a name.

Common situations: Passing a display name instead of the package identifier; copy-pasting a scoped name with a typo; using a local folder name that doesn't match the published name.

Related errors


AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13). Data as JSON: /api/errors/77476231e3fa2624. Report an issue: GitHub.