gohugoio/hugo · error
npm pack: failed to build: %w
Error message
npm pack: failed to build: %w
What it means
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.
Source
Thrown at modules/npm/package_builder.go:175
// Process collected entries: for each module, prefer package.hugo.json
// over package.json at the root level. Workspace package.json files are always processed.
for _, e := range entries {
m := e.info.Meta()
// Skip root-level package.json if this module has package.hugo.json.
if !e.isHugoJSON && e.isRootLevel && modulesWithHugoJSON[m.ModulePath()] {
continue
}
f, err := m.Open()
if err != nil {
return fmt.Errorf("npm pack: failed to open package file: %w", err)
}
b.Add(m.ModulePath(), f)
f.Close()
}
if b.Err() != nil {
return fmt.Errorf("npm pack: failed to build: %w", b.Err())
}
// 4. Build the autogenerated workspace package.json.
// Exclude deps already defined by the project itself — they don't
// need to be duplicated in the workspace and it simplifies maintenance.
moduleDeps := make(map[string]any)
moduleDepsComments := make(map[string]any)
for k, v := range b.dependencies {
if b.dependenciesComments[k] != "project" {
moduleDeps[k] = v
moduleDepsComments[k] = b.dependenciesComments[k]
}
}
moduleDevDeps := make(map[string]any)
moduleDevDepsComments := make(map[string]any)
for k, v := range b.devDependencies {
if b.devDependenciesComments[k] != "project" {
moduleDevDeps[k] = vView on GitHub ↗ (pinned to 8a468df065)
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.
Example fix
// module's package.hugo.json — before
{ "dependencies": ["postcss"] }
// after
{ "dependencies": { "postcss": "^8.4.0" } } Defensive patterns
Strategy: try-catch
Try / catch
if err := packBuilder.Build(); err != nil {
// wrapped root cause is inside; unwrap for actionable message
log.Printf("npm pack failed: %v", err)
var jsonErr *json.SyntaxError
if errors.As(err, &jsonErr) {
log.Printf("malformed JSON in a module's package.hugo.json")
}
os.Exit(1)
} Prevention
- 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
When it happens
Trigger: `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.
Common situations: 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.
Related errors
- npm pack: failed to parse package.json: %w
- npm pack: failed to open package file: %w
- npm pack: unsupported workspaces object; missing "packages"
- npm pack: unsupported workspaces type %T
- npm pack: malformed package.json
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/e169a862347ed134.json.
Report an issue: GitHub ↗.