{"id":"36eb3211352ddd03","repo":"gohugoio/hugo","slug":"npm-pack-failed-to-parse-package-json-w","errorCode":null,"errorMessage":"npm pack: failed to parse package.json: %w","messagePattern":"npm pack: failed to parse package\\.json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":73,"sourceCode":"\t\tdevDependencies:         make(map[string]any),\n\t\tdevDependenciesComments: make(map[string]any),\n\t\tdependencies:            make(map[string]any),\n\t\tdependenciesComments:    make(map[string]any),\n\t}\n\n\tworkspacePath := filepath.ToSlash(files.FolderPackagesHugoAutoGen)\n\n\t// skip modules that shouldn't have their package files processed, either because they are the project module (handled separately)\n\t// or because their UsePackageJSON setting disables it.\n\tskipPackageJSON := buildSkipPackageJSON(mods)\n\n\t// 1. Read project deps: prefer package.hugo.json, fall back to package.json.\n\tvar rootPkg map[string]any\n\trootData, err := afero.ReadFile(sourceFs, packageJSONName)\n\tif err == nil {\n\t\trootPkg = b.unmarshal(bytes.NewReader(rootData))\n\t\tif b.err != nil {\n\t\t\treturn fmt.Errorf(\"npm pack: failed to parse package.json: %w\", b.err)\n\t\t}\n\t}\n\n\t// Workspaces source: prefer package.hugo.json, fall back to package.json.\n\tvar workspacesSource map[string]any\n\thugoData, hugoErr := afero.ReadFile(sourceFs, files.FilenamePackageHugoJSON)\n\tif hugoErr == nil {\n\t\thugoPkg := b.unmarshal(bytes.NewReader(hugoData))\n\t\tif b.err != nil {\n\t\t\treturn fmt.Errorf(\"npm pack: failed to parse %s: %w\", files.FilenamePackageHugoJSON, b.err)\n\t\t}\n\t\tb.addm(\"project\", hugoPkg)\n\t\tworkspacesSource = hugoPkg\n\t} else if rootPkg != nil {\n\t\tb.addm(\"project\", rootPkg)\n\t\tworkspacesSource = rootPkg\n\t}\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/npm/package_builder.go#L55-L91","documentation":"During `hugo mod npm pack`, Hugo reads the project's root package.json (step 1 of Pack, and again in ensureWorkspaceRef) to merge dependencies and register its autogenerated workspace. If the file exists but is not valid JSON, unmarshalling fails and packing aborts with this wrapped parse error.","triggerScenarios":"Running `hugo mod npm pack` (or a build path that triggers npm packing) with a root package.json containing invalid JSON — trailing commas, comments, unquoted keys, merge-conflict markers, or truncated content.","commonSituations":"Hand-edited package.json with a trailing comma or `//` comment (JSON5 habits); unresolved git merge conflict markers; a file corrupted by a partial write or wrong encoding (UTF-16/BOM issues).","solutions":["Validate the root package.json with `jq . package.json` (or `node -e \"JSON.parse(require('fs').readFileSync('package.json'))\"`) and fix the reported syntax error.","Remove comments/trailing commas — package.json must be strict JSON.","Check for and resolve git merge-conflict markers (`<<<<<<<`) in the file.","If the file is beyond repair, regenerate it (`npm init -y`) and re-add your dependencies, then re-run `hugo mod npm pack`."],"exampleFix":"// before (package.json)\n{\n  \"dependencies\": {\n    \"tailwindcss\": \"^3.4.0\", // css framework\n  },\n}\n// after\n{\n  \"dependencies\": {\n    \"tailwindcss\": \"^3.4.0\"\n  }\n}","handlingStrategy":"validation","validationCode":"if data, err := os.ReadFile(\"package.json\"); err == nil {\n    var v map[string]any\n    if err := json.Unmarshal(data, &v); err != nil {\n        return fmt.Errorf(\"package.json is invalid JSON: %w\", err)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := npm.Pack(fs, deps); err != nil {\n    var jsonErr *json.SyntaxError\n    if errors.As(err, &jsonErr) {\n        log.Fatalf(\"fix package.json syntax near offset %d: %v\", jsonErr.Offset, err)\n    }\n    return err\n}","preventionTips":["Run `node -e \"require('./package.json')\"` or a JSON linter in pre-commit hooks","Never hand-edit package.json with trailing commas or comments — JSON, not JSON5","Let `npm install`/`npm pkg set` write the file instead of editing it manually"],"tags":["hugo","npm-pack","package-json","json-parse"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}