{"id":"a86e67ee92de62e2","repo":"vitejs/vite","slug":"name-in-package-json-is-required-if-option-build","errorCode":null,"errorMessage":"Name in package.json is required if option \"build.lib.fileName\" is not provided.","messagePattern":"Name in package\\.json is required if option \"build\\.lib\\.fileName\" is not provided\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/build.ts","lineNumber":1031,"sourceCode":"  format: ModuleFormat,\n  entryName: string,\n  root: string,\n  extension?: JsExt,\n  packageCache?: PackageCache,\n): string {\n  if (typeof libOptions.fileName === 'function') {\n    return libOptions.fileName(format, entryName)\n  }\n\n  const packageJson = findNearestMainPackageData(root, packageCache)?.data\n  const name =\n    libOptions.fileName ||\n    (packageJson && typeof libOptions.entry === 'string'\n      ? getPkgName(packageJson.name)\n      : entryName)\n\n  if (!name)\n    throw new Error(\n      'Name in package.json is required if option \"build.lib.fileName\" is not provided.',\n    )\n\n  extension ??= resolveOutputJsExtension(format, packageJson?.type)\n\n  if (format === 'cjs' || format === 'es') {\n    return `${name}.${extension}`\n  }\n\n  return `${name}.${format}.${extension}`\n}\n\nexport function resolveBuildOutputs(\n  outputs: OutputOptions | OutputOptions[] | undefined,\n  libOptions: LibraryOptions | false,\n  logger: Logger,\n): OutputOptions | OutputOptions[] | undefined {\n  if (libOptions) {","sourceCodeStart":1013,"sourceCodeEnd":1049,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/build.ts#L1013-L1049","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a 'name' field to the nearest package.json.","Or set build.lib.fileName explicitly (string or function) to control the output name.","If using a single string entry, confirm the package.json nearest to root has a valid name."],"exampleFix":"// before\n// package.json has no \"name\"\nbuild: { lib: { entry: { a: 'src/a.ts', b: 'src/b.ts' }, formats: ['es'] } }\n// after\nbuild: { lib: { entry: { a: 'src/a.ts', b: 'src/b.ts' }, formats: ['es'], fileName: 'my-lib' } }","handlingStrategy":"validation","validationCode":"const pkg = require('./package.json')\nconst lib = config.build?.lib\nconst needsName = lib && !lib.fileName &&\n  (typeof lib.entry !== 'string') && !pkg.name\nif (needsName) throw new Error('Set package.json name or build.lib.fileName')","typeGuard":"function canResolveLibFilename(lib: any, pkgName?: string): boolean {\n  return Boolean(lib.fileName || pkgName || typeof lib.entry === 'string')\n}","tryCatchPattern":null,"preventionTips":["Always set a 'name' field in package.json for libraries.","Provide build.lib.fileName explicitly to be independent of package.json.","For multi-entry libs, remember the package-name fallback only applies to single string entries."],"tags":["build","library-mode","package-json","output"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}