gohugoio/hugo · error
npm pack: failed to marshal JSON: %w
Error message
npm pack: failed to marshal JSON: %w
What it means
Hugo's `hugo mod npm pack` command merges package.json files from the project and its Hugo Modules dependencies, then writes the result with a JSON encoder. This error wraps a failure of `json.Encoder.Encode` in `writeJSON` (modules/npm/package_builder.go:362-371), meaning the assembled package data could not be serialized to JSON before writing package.json or package.hugo.json.
Source
Thrown at modules/npm/package_builder.go:368
}
func detectIndent(data []byte) string {
for line := range bytes.SplitSeq(data, []byte("\n")) {
trimmed := bytes.TrimLeft(line, " \t")
if len(trimmed) < len(line) && len(trimmed) > 0 && trimmed[0] == '"' {
return string(line[:len(line)-len(trimmed)])
}
}
return " "
}
func writeJSON(fs afero.Fs, filename string, v any) error {
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", strings.Repeat(" ", 2))
if err := encoder.Encode(v); err != nil {
return fmt.Errorf("npm pack: failed to marshal JSON: %w", err)
}
return afero.WriteFile(fs, filename, buf.Bytes(), 0o666)
}
func toStringSlice(v any) []string {
if v == nil {
return nil
}
if s, ok := v.([]any); ok {
var out []string
for _, item := range s {
if str, ok := item.(string); ok {
out = append(out, str)
}
}
return out
}
return nilView on GitHub ↗ (pinned to 8a468df065)
Solutions
- Validate the project's package.json and package.hugo.json with a JSON linter (e.g. `jq . package.hugo.json`) and fix malformed values.
- Check package.hugo.json files in imported modules/themes (under _vendor or the module cache) for invalid values and update or replace the offending module.
- Run `hugo mod clean` and `hugo mod get -u` to refresh the module cache, then retry `hugo mod npm pack`.
- If it persists, minimize the merged inputs to isolate which file introduces the unmarshalable value and report it upstream.
Defensive patterns
Strategy: validation
Validate before calling
// package.hugo.json must be valid JSON before build jq . package.hugo.json && jq . package.json
Prevention
- Validate package.hugo.json and package.json with jq or an editor JSON linter
- Avoid non-serializable or cyclic structures in npm config merged by hugo mod npm pack
- Run `hugo mod npm pack` after any dependency config change and check the error output
When it happens
Trigger: Running `hugo mod npm pack` where the merged package data contains a value Go's encoding/json cannot marshal — e.g. a NaN/Inf float, a cyclic structure, or a non-string map key introduced from a malformed package.hugo.json in the project or an imported module.
Common situations: A theme or module ships a package.hugo.json with unusual values that survive decoding but fail re-encoding; hand-edited package.hugo.json files; module version upgrades that change the dependency metadata being merged. In practice this is rare because JSON-decoded data normally round-trips cleanly.
Related errors
- unsupported format: %q
- must not be run from the file system root
- failed to detect format from content
- too many arguments to jsonify
- unmarshal failed: %w
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/af9ca3678dff6dda.json.
Report an issue: GitHub ↗.