{"record":{"id":"e169a862347ed134","repo":"gohugoio/hugo","slug":"npm-pack-failed-to-build-w","errorCode":null,"errorMessage":"npm pack: failed to build: %w","messagePattern":"npm pack: failed to build: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":175,"sourceCode":"\t// Process collected entries: for each module, prefer package.hugo.json\n\t// over package.json at the root level. Workspace package.json files are always processed.\n\tfor _, e := range entries {\n\t\tm := e.info.Meta()\n\t\t// Skip root-level package.json if this module has package.hugo.json.\n\t\tif !e.isHugoJSON && e.isRootLevel && modulesWithHugoJSON[m.ModulePath()] {\n\t\t\tcontinue\n\t\t}\n\n\t\tf, err := m.Open()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"npm pack: failed to open package file: %w\", err)\n\t\t}\n\t\tb.Add(m.ModulePath(), f)\n\t\tf.Close()\n\t}\n\n\tif b.Err() != nil {\n\t\treturn fmt.Errorf(\"npm pack: failed to build: %w\", b.Err())\n\t}\n\n\t// 4. Build the autogenerated workspace package.json.\n\t// Exclude deps already defined by the project itself — they don't\n\t// need to be duplicated in the workspace and it simplifies maintenance.\n\tmoduleDeps := make(map[string]any)\n\tmoduleDepsComments := make(map[string]any)\n\tfor k, v := range b.dependencies {\n\t\tif b.dependenciesComments[k] != \"project\" {\n\t\t\tmoduleDeps[k] = v\n\t\t\tmoduleDepsComments[k] = b.dependenciesComments[k]\n\t\t}\n\t}\n\tmoduleDevDeps := make(map[string]any)\n\tmoduleDevDepsComments := make(map[string]any)\n\tfor k, v := range b.devDependencies {\n\t\tif b.devDependenciesComments[k] != \"project\" {\n\t\t\tmoduleDevDeps[k] = v","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/modules/npm/package_builder.go#L157-L193","documentation":"In npm.Pack (modules/npm/package_builder.go:175). After processing all collected package entries via b.Add, if the builder accumulated any error in b.err it is wrapped as 'failed to build'. This is the aggregate/catch-all for a parse failure that occurred while adding a module's package file during the walk phase.","triggerScenarios":"Any module package file (package.json or package.hugo.json mounted under _jsconfig) fails json.Unmarshal inside b.Add -> b.unmarshal, and the error surfaces here rather than at a specific earlier check.","commonSituations":"A theme/module ships a malformed package.json that passes the existence checks but fails JSON parsing; a stale module cache with a half-written package.json; a module whose package.hugo.json was hand-edited incorrectly.","solutions":["Inspect the module package files (the walk covers _jsconfig mounts of package.json/package.hugo.json) and validate each as JSON.","Run 'hugo mod clean' and 'hugo mod tidy' to refresh modules.","Temporarily set the offending module's usePackageJSON to 'never' to skip it."],"exampleFix":"// before: a theme module ships\n{ \"name\": \"theme\", \"dependencies\": { \"x\": } }\n// -> 'npm pack: failed to build: invalid character '}' ...'\n\n// after: fix the theme's package.json (or upgrade the theme)\n{ \"name\": \"theme\", \"dependencies\": { \"x\": \"^1.0\" } }","handlingStrategy":"validation","validationCode":"// validate every module's package.json/package.hugo.json as JSON\nfor _, m := range modules {\n    for _, name := range []string{\"package.json\", \"package.hugo.json\"} {\n        p := filepath.Join(m.Dir(), name)\n        if data, err := os.ReadFile(p); err == nil {\n            var v any\n            if err := json.Unmarshal(data, &v); err != nil {\n                return fmt.Errorf(\"%s: %w\", p, err)\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate module package files after 'hugo mod tidy'.","Set usePackageJSON='never' on modules you don't want merged.","Report broken package files to the module/theme author."],"tags":["hugo","npm-pack","json"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}