{"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":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/plugins/css.ts","lineNumber":3668,"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":3650,"sourceCodeEnd":3674,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/plugins/css.ts#L3650-L3674","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before — package.json has no name\n{ \"version\": \"1.0.0\" }\n// vite.config.ts\nexport default defineConfig({ build: { lib: { entry: 'src/index.ts' } } });\n// after — package.json\n{ \"name\": \"my-lib\", \"version\": \"1.0.0\" }","handlingStrategy":"validation","validationCode":"import { readFileSync, existsSync } from 'node:fs';\nimport path from 'node:path';\n\nfunction validateLibCssName(root, lib) {\n  if (lib?.cssFileName || lib?.fileName) return;\n  const pkgPath = path.join(root, 'package.json');\n  if (existsSync(pkgPath)) {\n    const pkg = JSON.parse(readFileSync(pkgPath,'utf8'));\n    if (!pkg.name) throw new Error('package.json needs a \"name\" for library CSS output');\n  } else {\n    throw new Error('No package.json found — set build.lib.cssFileName');\n  }\n}\n// validateLibCssName(config.root, config.build.lib);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["css","library-build","config","package-json"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}