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] = v

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Identify which module's package file is broken: validate each theme/module's package.json and package.hugo.json with `jq .`.
  2. Fix or report the malformed file upstream; ensure `dependencies`/`devDependencies` are JSON objects mapping name→version.
  3. 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

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


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/e169a862347ed134.json. Report an issue: GitHub ↗.