{"record":{"id":"06373b79e91bab40","repo":"vitejs/vite","slug":"name-in-package-json-is-required-if-option-build-06373b","errorCode":null,"errorMessage":"Name in package.json is required if option \"build.lib.cssFileName\" is not provided.","messagePattern":"Name in package\\.json is required if option \"build\\.lib\\.cssFileName\" is not provided\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/plugins/css.ts","lineNumber":3674,"sourceCode":"  return targets\n}\n\nexport function resolveLibCssFilename(\n  libOptions: LibraryOptions,\n  root: string,\n  packageCache?: PackageCache,\n): string {\n  if (typeof libOptions.cssFileName === 'string') {\n    return `${libOptions.cssFileName}.css`\n  } else if (typeof libOptions.fileName === 'string') {\n    return `${libOptions.fileName}.css`\n  }\n\n  const packageJson = findNearestMainPackageData(root, packageCache)?.data\n  const name = packageJson ? getPkgName(packageJson.name) : undefined\n\n  if (!name)\n    throw new Error(\n      'Name in package.json is required if option \"build.lib.cssFileName\" is not provided.',\n    )\n\n  return `${name}.css`\n}\n","sourceCodeStart":3656,"sourceCodeEnd":3680,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/plugins/css.ts#L3656-L3680","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set a name in your project's package.json (the nearest one to the build root).","Explicitly set build.lib.cssFileName in vite.config (e.g. cssFileName: 'my-lib') to bypass the package.json lookup entirely.","Alternatively set build.lib.fileName so the same stem is reused for the CSS output.","If in a monorepo, verify findNearestMainPackageData resolves to the leaf package.json you expect, not a root without a name."],"exampleFix":"// before (vite.config.ts)\nbuild: { lib: { entry: 'src/index.ts', formats: ['es'] } }\n// package.json has no \"name\"\n// after\n// package.json\n{ \"name\": \"my-lib\", \"version\": \"0.0.0\" }\n// or vite.config.ts\nbuild: { lib: { entry: 'src/index.ts', formats: ['es'], cssFileName: 'my-lib' } }","handlingStrategy":"validation","validationCode":"// Before build, verify a CSS filename can be derived\nimport { readFileSync, existsSync } from 'node:fs'\nfunction assertLibCssName(lib, root) {\n  if (typeof lib?.cssFileName === 'string' || typeof lib?.fileName === 'string') return\n  const tryRead = (p) => existsSync(p) ? JSON.parse(readFileSync(p, 'utf8')) : null\n  const pkg = tryRead(`${root}/package.json`) || tryRead(`${process.cwd()}/package.json`)\n  if (!pkg?.name) throw new Error('Set package.json name or build.lib.cssFileName')\n}\nassertLibCssName(config.build?.lib, config.root)","typeGuard":"function hasLibCssNameSource(lib, pkgName): boolean {\n  return typeof lib?.cssFileName === 'string'\n    || typeof lib?.fileName === 'string'\n    || typeof pkgName === 'string' && pkgName.length > 0\n}","tryCatchPattern":null,"preventionTips":["Always set a name in package.json for library projects.","When scripting builds, pass build.lib.cssFileName explicitly to remove dependence on package.json.","Validate the lib config in a prebuild script before invoking vite build."],"tags":["build","library-mode","css","config"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}