gohugoio/hugo · error
npm pack: failed to parse package.json
Error message
npm pack: failed to parse package.json: %w
What it means
In npm.Pack (modules/npm/package_builder.go:73). The project-root package.json is read (afero.ReadFile succeeded) and handed to the packageBuilder.unmarshal, which runs json.Unmarshal. If that fails, b.err holds the JSON error and Pack returns it wrapped. This is the project-level package.json that Hugo uses to seed the autogenerated workspace package.
Solutions
- Validate package.json with a JSON linter or 'npm install' (npm will report the syntax error and location).
- Remove trailing commas, comments, and single quotes; ensure keys are double-quoted.
- If package.json is generated, regenerate it from source.
Example fix
// before
{
"name": "proj",
"scripts": { "dev": "vite", } // trailing comma
}
// after
{
"name": "proj",
"scripts": { "dev": "vite" }
} Defensive patterns
Strategy: validation
Validate before calling
// validate package.json before invoking Hugo npm pack
data, err := os.ReadFile("package.json")
if err == nil {
var v any
if err := json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("package.json invalid: %w", err)
}
} Prevention
- Run 'npm install' to surface JSON errors early.
- Keep package.json hand-edit-free; use npm scripts to mutate it.
- Add a JSON lint step to CI.
When it happens
Trigger: The root package.json exists but is not valid JSON (trailing comma, single quotes, unquoted keys, BOM, truncation, or a merge-conflict marker left in).
Common situations: Manual edits to package.json; a botched npm install / merge conflict; a file generated by another tool with non-strict JSON; copy-pasting JS-style comments into package.json.
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 build
- npm pack: failed to marshal package.json with updated…
- npm pack: failed to parse
- npm pack: malformed package.json
- npm pack: could not locate existing workspaces array for…
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/36eb3211352ddd03.
Report an issue: GitHub.
Appendix: source
Thrown at modules/npm/package_builder.go:73
devDependencies: make(map[string]any),
devDependenciesComments: make(map[string]any),
dependencies: make(map[string]any),
dependenciesComments: make(map[string]any),
}
workspacePath := filepath.ToSlash(files.FolderPackagesHugoAutoGen)
// skip modules that shouldn't have their package files processed, either because they are the project module (handled separately)
// or because their UsePackageJSON setting disables it.
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
}
View on GitHub (pinned to 52c9bd7908)