vitejs/vite · error · Error
Name in package.json is required if option…
Error message
Name in package.json is required if option "build.lib.fileName" is not provided.
What it means
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.
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.
Example fix
// before (vite.config.js)
build: { lib: { entry: 'src/index.ts', formats: ['es'] } }
// after
build: { lib: { entry: 'src/index.ts', fileName: 'my-lib', formats: ['es'] } }
// or in package.json: { "name": "my-lib" } Defensive patterns
Strategy: validation
Validate before calling
const fs = require('fs');
function ensureLibNameOrFileName(libOptions, root) {
if (libOptions?.fileName) return true;
if (libOptions && typeof libOptions.entry === 'object') return true;
try {
const pkg = JSON.parse(fs.readFileSync(require('path').join(root, 'package.json'), 'utf8'));
return Boolean(pkg && pkg.name);
} catch { return false; }
} Type guard
function libHasNameOrFileName(libOptions) {
return Boolean(libOptions && (libOptions.fileName || (typeof libOptions.entry === 'object' && libOptions.entry !== null)));
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Multiple entry points are not supported when output formats…
- Option "build.lib.name" is required when output formats…
- Environment " " is not defined in the config.
- `input` cannot contain glob characters. They are reserved…
- Name in package.json is required if option…
AI-assisted analysis of vitejs/vite@b4d66fee14 (2026-08-11).
Data as JSON: /api/errors/a86e67ee92de62e2.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vite/src/node/build.ts:1035
format: ModuleFormat,
entryName: string,
root: string,
extension?: JsExt,
packageCache?: PackageCache,
): string {
if (typeof libOptions.fileName === 'function') {
return libOptions.fileName(format, entryName)
}
const packageJson = findNearestMainPackageData(root, packageCache)?.data
const name =
libOptions.fileName ||
(packageJson && typeof libOptions.entry === 'string'
? getPkgName(packageJson.name)
: entryName)
if (!name)
throw new Error(
'Name in package.json is required if option "build.lib.fileName" is not provided.',
)
extension ??= resolveOutputJsExtension(format, packageJson?.type)
if (format === 'cjs' || format === 'es') {
return `${name}.${extension}`
}
return `${name}.${format}.${extension}`
}
export function resolveBuildOutputs(
outputs: OutputOptions | OutputOptions[] | undefined,
libOptions: LibraryOptions | false,
logger: Logger,
): OutputOptions | OutputOptions[] | undefined {
if (libOptions) {View on GitHub (pinned to b4d66fee14)