{"id":"af9ca3678dff6dda","repo":"gohugoio/hugo","slug":"npm-pack-failed-to-marshal-json-w","errorCode":null,"errorMessage":"npm pack: failed to marshal JSON: %w","messagePattern":"npm pack: failed to marshal JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":368,"sourceCode":"}\n\nfunc detectIndent(data []byte) string {\n\tfor line := range bytes.SplitSeq(data, []byte(\"\\n\")) {\n\t\ttrimmed := bytes.TrimLeft(line, \" \\t\")\n\t\tif len(trimmed) < len(line) && len(trimmed) > 0 && trimmed[0] == '\"' {\n\t\t\treturn string(line[:len(line)-len(trimmed)])\n\t\t}\n\t}\n\treturn \"  \"\n}\n\nfunc writeJSON(fs afero.Fs, filename string, v any) error {\n\tbuf := new(bytes.Buffer)\n\tencoder := json.NewEncoder(buf)\n\tencoder.SetEscapeHTML(false)\n\tencoder.SetIndent(\"\", strings.Repeat(\" \", 2))\n\tif err := encoder.Encode(v); err != nil {\n\t\treturn fmt.Errorf(\"npm pack: failed to marshal JSON: %w\", err)\n\t}\n\treturn afero.WriteFile(fs, filename, buf.Bytes(), 0o666)\n}\n\nfunc toStringSlice(v any) []string {\n\tif v == nil {\n\t\treturn nil\n\t}\n\tif s, ok := v.([]any); ok {\n\t\tvar out []string\n\t\tfor _, item := range s {\n\t\t\tif str, ok := item.(string); ok {\n\t\t\t\tout = append(out, str)\n\t\t\t}\n\t\t}\n\t\treturn out\n\t}\n\treturn nil","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/npm/package_builder.go#L350-L386","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// package.hugo.json must be valid JSON before build\njq . package.hugo.json && jq . package.json","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["hugo","hugo-modules","npm","json","serialization"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}