vitejs/vite · error · Error

Name in package.json is required if option "build.lib.fileNa

Error message

Name in package.json is required if option "build.lib.fileName" is not provided.

What it means

In resolveLibFilename (build.ts:1030), Vite computes the output filename for library mode. If libOptions.fileName is not provided, it falls back to the package name (getPkgName) when entry is a string, otherwise the entry alias. If none yields a name, it throws because there is no basis for naming the output file.

Source

Thrown at packages/vite/src/node/build.ts:1031

  format: ModuleFormat,
  entryName: string,
  root: string,
  extension?: JsExt,
  packageCache?: PackageCache,
): string {
  if (typeof libOptions.fileName === 'function') {
    return libOptions.fileName(format, entryName)
  }

  const packageJson = findNearestMainPackageData(root, packageCache)?.data
  const name =
    libOptions.fileName ||
    (packageJson && typeof libOptions.entry === 'string'
      ? getPkgName(packageJson.name)
      : entryName)

  if (!name)
    throw new Error(
      'Name in package.json is required if option "build.lib.fileName" is not provided.',
    )

  extension ??= resolveOutputJsExtension(format, packageJson?.type)

  if (format === 'cjs' || format === 'es') {
    return `${name}.${extension}`
  }

  return `${name}.${format}.${extension}`
}

export function resolveBuildOutputs(
  outputs: OutputOptions | OutputOptions[] | undefined,
  libOptions: LibraryOptions | false,
  logger: Logger,
): OutputOptions | OutputOptions[] | undefined {
  if (libOptions) {

View on GitHub (pinned to 89620f09af)

Solutions

  1. Add a 'name' field to the nearest package.json.
  2. Or set build.lib.fileName explicitly (string or function) to control the output name.
  3. If using a single string entry, confirm the package.json nearest to root has a valid name.

Example fix

// before
// package.json has no "name"
build: { lib: { entry: { a: 'src/a.ts', b: 'src/b.ts' }, formats: ['es'] } }
// after
build: { lib: { entry: { a: 'src/a.ts', b: 'src/b.ts' }, formats: ['es'], fileName: 'my-lib' } }
Defensive patterns

Strategy: validation

Validate before calling

const pkg = require('./package.json')
const lib = config.build?.lib
const needsName = lib && !lib.fileName &&
  (typeof lib.entry !== 'string') && !pkg.name
if (needsName) throw new Error('Set package.json name or build.lib.fileName')

Type guard

function canResolveLibFilename(lib: any, pkgName?: string): boolean {
  return Boolean(lib.fileName || pkgName || typeof lib.entry === 'string')
}

Prevention

When it happens

Trigger: Building in library mode without build.lib.fileName, with a multi-entry (object) entry map, and no name field (or an unreadable name) in the nearest package.json.

Common situations: Library builds where package.json lacks a 'name' field (common in private workspaces), or entry is an object so the package-name fallback path is skipped.

Related errors


AI-assisted analysis of vitejs/vite@89620f09af (2026-08-03). Data as JSON: /data/errors/a86e67ee92de62e2.json. Report an issue: GitHub.