vitejs/vite · error · Error
Name in package.json is required if option "build.lib.cssFil
Error message
Name in package.json is required if option "build.lib.cssFileName" is not provided.
What it means
Thrown by `resolveLibCssFilename` when building a library (`build.lib`) and none of `lib.cssFileName`, `lib.fileName`, or a `name` field in the nearest `package.json` is available to derive the output CSS filename. Vite needs a name to produce `<name>.css` when you have not given an explicit one.
Source
Thrown at packages/vite/src/node/plugins/css.ts:3668
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 89620f09af)
Solutions
- Add a `name` field to the nearest `package.json` (e.g. `"name": "my-lib"`).
- Set `build.lib.cssFileName` explicitly to bypass name inference.
- Set `build.lib.fileName` (it is also used to derive the CSS filename).
Example fix
// before — package.json has no name
{ "version": "1.0.0" }
// vite.config.ts
export default defineConfig({ build: { lib: { entry: 'src/index.ts' } } });
// after — package.json
{ "name": "my-lib", "version": "1.0.0" } Defensive patterns
Strategy: validation
Validate before calling
import { readFileSync, existsSync } from 'node:fs';
import path from 'node:path';
function validateLibCssName(root, lib) {
if (lib?.cssFileName || lib?.fileName) return;
const pkgPath = path.join(root, 'package.json');
if (existsSync(pkgPath)) {
const pkg = JSON.parse(readFileSync(pkgPath,'utf8'));
if (!pkg.name) throw new Error('package.json needs a "name" for library CSS output');
} else {
throw new Error('No package.json found — set build.lib.cssFileName');
}
}
// validateLibCssName(config.root, config.build.lib); Prevention
- Always set `name` in `package.json` for library packages.
- Set `build.lib.cssFileName` explicitly to avoid reliance on name inference.
- Run library builds in CI to catch missing-name errors early.
When it happens
Trigger: Running a library build (`build.lib` truthy) where the package produces CSS, `build.lib.cssFileName` and `build.lib.fileName` are unset, and the nearest `package.json` has no `name` field (or there is no package.json at all).
Common situations: A new package scaffolded without a `name` in `package.json`; a build run from a directory whose nearest `package.json` is a stub; a monorepo package whose `name` was removed during refactoring.
Related errors
- When "build.cssCodeSplit: false" is set, "rolldownOptions.in
- Failed to load PostCSS config: ${e}
- Unsupported target "${e}"
- Unsupported target "${entry}"
- { runtime: "${result.runtime}" } is not supported for assets
AI-assisted analysis of vitejs/vite@89620f09af (2026-08-03).
Data as JSON: /data/errors/06373b79e91bab40.json.
Report an issue: GitHub.