gohugoio/hugo · error
npm pack: failed to parse %s: %w
Error message
npm pack: failed to parse %s: %w
What it means
In npm.Pack (modules/npm/package_builder.go:83). When package.hugo.json (files.FilenamePackageHugoJSON) is present it is parsed preferentially; a JSON parse failure is wrapped with the filename. package.hugo.json is the Hugo-flavored package descriptor that lets modules declare npm dependencies.
Source
Thrown at modules/npm/package_builder.go:83
skipPackageJSON := buildSkipPackageJSON(mods)
// 1. Read project deps: prefer package.hugo.json, fall back to package.json.
var rootPkg map[string]any
rootData, err := afero.ReadFile(sourceFs, packageJSONName)
if err == nil {
rootPkg = b.unmarshal(bytes.NewReader(rootData))
if b.err != nil {
return fmt.Errorf("npm pack: failed to parse package.json: %w", b.err)
}
}
// Workspaces source: prefer package.hugo.json, fall back to package.json.
var workspacesSource map[string]any
hugoData, hugoErr := afero.ReadFile(sourceFs, files.FilenamePackageHugoJSON)
if hugoErr == nil {
hugoPkg := b.unmarshal(bytes.NewReader(hugoData))
if b.err != nil {
return fmt.Errorf("npm pack: failed to parse %s: %w", files.FilenamePackageHugoJSON, b.err)
}
b.addm("project", hugoPkg)
workspacesSource = hugoPkg
} else if rootPkg != nil {
b.addm("project", rootPkg)
workspacesSource = rootPkg
}
// 2. Read deps from referenced workspaces (always from package.json).
for _, wsDir := range resolveProjectWorkspaces(sourceFs, workspacesSource, workspacePath) {
wsFile := filepath.Join(wsDir, packageJSONName)
wsData, err := afero.ReadFile(sourceFs, wsFile)
if err != nil {
continue
}
wsm := b.unmarshal(bytes.NewReader(wsData))
if b.err != nil {
return fmt.Errorf("npm pack: failed to parse %s: %w", wsFile, b.err)View on GitHub (pinned to 52c9bd7908)
Solutions
- Run the file through a JSON validator and fix the reported location.
- Compare against a known-good package.hugo.json (same shape as package.json: dependencies, devDependencies, workspaces).
- Delete package.hugo.json if you do not need Hugo-specific package merging (Hugo falls back to package.json).
Example fix
// before: package.hugo.json
{
dependencies: { "jquery": "^3.0" } // unquoted key
}
// after
{
"dependencies": { "jquery": "^3.0" }
} Defensive patterns
Strategy: validation
Validate before calling
if data, err := os.ReadFile("package.hugo.json"); err == nil {
var v any
if err := json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("package.hugo.json invalid: %w", err)
}
} Prevention
- Treat package.hugo.json as strict JSON (no comments/trailing commas).
- Delete it if unused; Hugo falls back to package.json.
- Validate in CI alongside package.json.
When it happens
Trigger: A package.hugo.json file exists in the project root but contains invalid JSON.
Common situations: Hand-authoring package.hugo.json with syntax errors; partial git checkout; merging a theme's package.hugo.json incorrectly; converting from package.json and leaving invalid structure.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- npm pack: failed to parse package.json: %w
- npm pack: failed to build: %w
- npm pack: failed to marshal package.json with updated worksp
- npm pack: malformed package.json
- npm pack: failed to open package file: %w
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/318395944fd396dc.
Report an issue: GitHub.