yarnpkg/yarn · error · MessageError

unknownPackageName

Error message

unknownPackageName

What it means

When `yarn link` is run with no arguments, yarn registers the current working-directory module into the global link folder (link.js:53-56). It reads the root manifest and requires a `name`; a package.json without a `name` field throws unknownPackageName because yarn has nothing to name the symlink under config.linkFolder.

Source

Thrown at src/cli/commands/link.js:56

      if (await fs.exists(src)) {
        const folder = await getRegistryFolder(config, name);
        const dest = path.join(folder, name);

        await fs.unlink(dest);
        await fs.mkdirp(path.dirname(dest));
        await fs.symlink(src, dest);
        reporter.success(reporter.lang('linkUsing', name));
      } else {
        throw new MessageError(reporter.lang('linkMissing', name));
      }
    }
  } else {
    // add cwd module to the global 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)) {
      reporter.warn(reporter.lang('linkCollision', name));
    } else {
      await fs.mkdirp(path.dirname(linkLoc));
      await fs.symlink(config.cwd, 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 binSrc = manifest.bin[binName];
          const binSrcLoc = path.join(linkLoc, binSrc);
          const binDestLoc = path.join(globalBinFolder, binName);
          if (await fs.exists(binDestLoc)) {

View on GitHub (pinned to c2dda503f3)

Solutions

  1. Add a valid `name` field to the package.json in the current directory.
  2. Ensure you are running `yarn link` from the package root that contains the package.json you intend to register.
  3. Validate the manifest with `node -e "console.log(require('./package.json').name)"` before linking.

Example fix

// before — package.json
{
  "version": "1.0.0"
}
// after
{
  "name": "mylib",
  "version": "1.0.0"
}
Defensive patterns

Strategy: validation

Validate before calling

const pkg = require('./package.json');
if (!pkg.name) {
  throw new Error('package.json is missing a "name" field; cannot `yarn link`.');
}

Type guard

function hasName(pkg) {
  return typeof pkg === 'object' && pkg !== null && typeof pkg.name === 'string' && pkg.name.length > 0;
}

Try / catch

try {
  await runYarn(['link']);
} catch (e) {
  if (/unknownPackageName/.test(e.message)) {
    console.error('Add a "name" to package.json before registering for linking.');
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Running bare `yarn link` in a project whose package.json has no `name`, or whose package.json failed to load an identity (e.g., only a `package-lock.json`, or an incomplete scaffold).

Common situations: Fresh project initialized with `yarn init -y` but name left blank; monorepo sub-folder without its own package.json picking up a nameless root; typo deleting the `name` field.

Related errors


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