{"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/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/npm/package_builder.go#L157-L193","documentation":"The packageBuilder accumulates dependencies from every module's package.json/package.hugo.json and records the first error it hits internally (b.err). After all files are added, Pack checks b.Err() and aborts with this wrapper if any file failed to parse or merge. It's the deferred, aggregate form of a per-file failure — typically malformed JSON in one module's package file.","triggerScenarios":"`hugo mod npm pack` where any mounted module's package.json or package.hugo.json fails json.Unmarshal, or its `dependencies`/`devDependencies` values are not objects, causing the builder to record an error consumed at the end of the add loop.","commonSituations":"A theme shipping a malformed or JSON5-style package.hugo.json; a module whose package.json has dependencies declared as an array or string; upgrading a theme that introduced a bad package file.","solutions":["Identify which module's package file is broken: validate each theme/module's package.json and package.hugo.json with `jq .`.","Fix or report the malformed file upstream; ensure `dependencies`/`devDependencies` are JSON objects mapping name→version.","As a workaround, set `params.module... ` — disable packing for the offending module via its module config `noVendor`/`ignoreImports` alternatives or remove the module until fixed."],"exampleFix":"// module's package.hugo.json — before\n{ \"dependencies\": [\"postcss\"] }\n// after\n{ \"dependencies\": { \"postcss\": \"^8.4.0\" } }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := packBuilder.Build(); err != nil {\n    // wrapped root cause is inside; unwrap for actionable message\n    log.Printf(\"npm pack failed: %v\", err)\n    var jsonErr *json.SyntaxError\n    if errors.As(err, &jsonErr) {\n        log.Printf(\"malformed JSON in a module's package.hugo.json\")\n    }\n    os.Exit(1)\n}","preventionTips":["This is a wrapper error — read the wrapped cause (%w chain) to find the failing module's package file","Keep all package.hugo.json files in imported modules valid JSON","Pin module versions so an upstream module's broken package file doesn't break your build unexpectedly"],"tags":["hugo","npm-pack","package-json","modules"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}