{"record":{"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":1035,"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":1017,"sourceCodeEnd":1053,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/build.ts#L1017-L1053","documentation":"When building a library, Vite resolves the output filename from build.lib.fileName, the package.json name field, or the entry key name. If fileName is omitted AND the entry is a single string AND package.json has no name, resolveLibFilename has nothing to base the filename on and throws. The check lives in resolveLibFilename, called while generating per-format output names.","triggerScenarios":"Running vite build with build.lib configured (entry as a string) but no build.lib.fileName and either no package.json or a package.json missing the name field.","commonSituations":"Quick library prototypes started outside an npm package directory; monorepo sub-packages whose package.json name was deleted; CI builds that run in a copy of the repo without package.json.","solutions":["Add a \"name\" field to the nearest package.json (e.g. \"name\": \"my-lib\").","Alternatively, set build.lib.fileName explicitly: fileName: 'my-lib'.","If the entry is an object (multiple entries), the entry key is used as the fallback name, so switch to object-form entry."],"exampleFix":"// before (vite.config.js)\nbuild: { lib: { entry: 'src/index.ts', formats: ['es'] } }\n// after\nbuild: { lib: { entry: 'src/index.ts', fileName: 'my-lib', formats: ['es'] } }\n// or in package.json: { \"name\": \"my-lib\" }","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nfunction ensureLibNameOrFileName(libOptions, root) {\n  if (libOptions?.fileName) return true;\n  if (libOptions && typeof libOptions.entry === 'object') return true;\n  try {\n    const pkg = JSON.parse(fs.readFileSync(require('path').join(root, 'package.json'), 'utf8'));\n    return Boolean(pkg && pkg.name);\n  } catch { return false; }\n}","typeGuard":"function libHasNameOrFileName(libOptions) {\n  return Boolean(libOptions && (libOptions.fileName || (typeof libOptions.entry === 'object' && libOptions.entry !== null)));\n}","tryCatchPattern":null,"preventionTips":["Always set either a package.json name or build.lib.fileName for library builds.","Prefer object-form entry for multi-entry libs so the key name is the fallback."],"tags":["build","library","config","package-json"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}