yarnpkg/yarn · error · MessageError

unknownPackageName

Error message

unknownPackageName

What it means

Thrown in the no-args branch of 'yarn unlink' (which unregisters the current package from the global link folder) when the root manifest has no 'name' field. Without a name, Yarn cannot locate the package's entry in the global link folder.

Source

Thrown at src/cli/commands/unlink.js:37

export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
  if (args.length) {
    for (const name of args) {
      const linkLoc = path.join(config.linkFolder, name);
      if (await fs.exists(linkLoc)) {
        await fs.unlink(path.join(await getRegistryFolder(config, name), name));
        reporter.success(reporter.lang('linkDisusing', name));
        reporter.info(reporter.lang('linkDisusingMessage', name));
      } else {
        throw new MessageError(reporter.lang('linkMissing', name));
      }
    }
  } else {
    // remove from registry
    const manifest = await config.readRootManifest();
    const name = manifest.name;
    if (!name) {
      throw new MessageError(reporter.lang('unknownPackageName'));
    }

    const linkLoc = path.join(config.linkFolder, name);
    if (await fs.exists(linkLoc)) {
      // If there is a `bin` defined in the package.json,
      // link each bin to the global bin
      if (manifest.bin) {
        const globalBinFolder = await getGlobalBinFolder(config, flags);
        for (const binName in manifest.bin) {
          const binDestLoc = path.join(globalBinFolder, binName);
          if (await fs.exists(binDestLoc)) {
            await fs.unlink(binDestLoc);
            if (process.platform === 'win32') {
              await fs.unlink(binDestLoc + '.cmd');
            }
          }
        }
      }

View on GitHub (pinned to c2dda503f3)

Solutions

  1. Add a 'name' field to the root package.json.
  2. Alternatively, pass the package name explicitly: 'yarn unlink <name>'.
  3. Confirm you are in the package's own directory (where 'yarn link' was originally run).

Example fix

// before (package.json)
{ "version": "1.0.0" }
// after
{ "name": "my-pkg", "version": "1.0.0" }
Defensive patterns

Strategy: validation

Validate before calling

const manifest = await config.readRootManifest();
if (!manifest.name) {
  throw new Error('Cannot unlink: root package.json has no "name" field');
}

Type guard

function hasName(m: { name?: string }): m is { name: string } {
  return typeof m.name === 'string' && m.name.length > 0;
}

Prevention

When it happens

Trigger: Running bare 'yarn unlink' in a project whose root package.json has no 'name' field, so manifest.name is falsy.

Common situations: Uninitialized/private project without a name; running from a directory lacking a package.json; root of a monorepo whose root manifest intentionally omits 'name'.

Related errors


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