vitejs/vite · error · Error

Name in package.json is required if option…

Error message

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

What it means

Thrown by resolveLibCssFilename when Vite builds a library (build.lib) and cannot derive a name for the emitted CSS file. The function checks build.lib.cssFileName, then build.lib.fileName, then falls back to the 'name' field in the nearest package.json; if all three are missing it throws. It exists because Rollup needs an explicit output filename for the CSS asset in lib mode.

Solutions

  1. Set a name in your project's package.json (the nearest one to the build root).
  2. Explicitly set build.lib.cssFileName in vite.config (e.g. cssFileName: 'my-lib') to bypass the package.json lookup entirely.
  3. Alternatively set build.lib.fileName so the same stem is reused for the CSS output.
  4. If in a monorepo, verify findNearestMainPackageData resolves to the leaf package.json you expect, not a root without a name.

Example fix

// before (vite.config.ts)
build: { lib: { entry: 'src/index.ts', formats: ['es'] } }
// package.json has no "name"
// after
// package.json
{ "name": "my-lib", "version": "0.0.0" }
// or vite.config.ts
build: { lib: { entry: 'src/index.ts', formats: ['es'], cssFileName: 'my-lib' } }
Defensive patterns

Strategy: validation

Validate before calling

// Before build, verify a CSS filename can be derived
import { readFileSync, existsSync } from 'node:fs'
function assertLibCssName(lib, root) {
  if (typeof lib?.cssFileName === 'string' || typeof lib?.fileName === 'string') return
  const tryRead = (p) => existsSync(p) ? JSON.parse(readFileSync(p, 'utf8')) : null
  const pkg = tryRead(`${root}/package.json`) || tryRead(`${process.cwd()}/package.json`)
  if (!pkg?.name) throw new Error('Set package.json name or build.lib.cssFileName')
}
assertLibCssName(config.build?.lib, config.root)

Type guard

function hasLibCssNameSource(lib, pkgName): boolean {
  return typeof lib?.cssFileName === 'string'
    || typeof lib?.fileName === 'string'
    || typeof pkgName === 'string' && pkgName.length > 0
}

Prevention

When it happens

Trigger: Running `vite build` with build.lib enabled while neither build.lib.cssFileName nor build.lib.fileName is set, AND the project's package.json has no 'name' field (or no package.json is found by findNearestMainPackageData). Only triggered when the lib build actually produces CSS output.

Common situations: New library scaffolds without a package.json yet, monorepo packages built before publishing (name often set only at publish), or a package.json whose 'name' was deleted during local testing. Also seen when lib config is constructed programmatically and the package.json lookup resolves to a parent without a name.

Related errors


AI-assisted analysis of vitejs/vite@b4d66fee14 (2026-08-11). Data as JSON: /api/errors/06373b79e91bab40. Report an issue: GitHub.

Appendix: source

Thrown at packages/vite/src/node/plugins/css.ts:3674

  return targets
}

export function resolveLibCssFilename(
  libOptions: LibraryOptions,
  root: string,
  packageCache?: PackageCache,
): string {
  if (typeof libOptions.cssFileName === 'string') {
    return `${libOptions.cssFileName}.css`
  } else if (typeof libOptions.fileName === 'string') {
    return `${libOptions.fileName}.css`
  }

  const packageJson = findNearestMainPackageData(root, packageCache)?.data
  const name = packageJson ? getPkgName(packageJson.name) : undefined

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

  return `${name}.css`
}

View on GitHub (pinned to b4d66fee14)