gohugoio/hugo · error

npm pack: failed to parse %s: %w

Error message

npm pack: failed to parse %s: %w

What it means

In npm.Pack (modules/npm/package_builder.go:83). When package.hugo.json (files.FilenamePackageHugoJSON) is present it is parsed preferentially; a JSON parse failure is wrapped with the filename. package.hugo.json is the Hugo-flavored package descriptor that lets modules declare npm dependencies.

Source

Thrown at modules/npm/package_builder.go:83

	skipPackageJSON := buildSkipPackageJSON(mods)

	// 1. Read project deps: prefer package.hugo.json, fall back to package.json.
	var rootPkg map[string]any
	rootData, err := afero.ReadFile(sourceFs, packageJSONName)
	if err == nil {
		rootPkg = b.unmarshal(bytes.NewReader(rootData))
		if b.err != nil {
			return fmt.Errorf("npm pack: failed to parse package.json: %w", b.err)
		}
	}

	// Workspaces source: prefer package.hugo.json, fall back to package.json.
	var workspacesSource map[string]any
	hugoData, hugoErr := afero.ReadFile(sourceFs, files.FilenamePackageHugoJSON)
	if hugoErr == nil {
		hugoPkg := b.unmarshal(bytes.NewReader(hugoData))
		if b.err != nil {
			return fmt.Errorf("npm pack: failed to parse %s: %w", files.FilenamePackageHugoJSON, b.err)
		}
		b.addm("project", hugoPkg)
		workspacesSource = hugoPkg
	} else if rootPkg != nil {
		b.addm("project", rootPkg)
		workspacesSource = rootPkg
	}

	// 2. Read deps from referenced workspaces (always from package.json).
	for _, wsDir := range resolveProjectWorkspaces(sourceFs, workspacesSource, workspacePath) {
		wsFile := filepath.Join(wsDir, packageJSONName)
		wsData, err := afero.ReadFile(sourceFs, wsFile)
		if err != nil {
			continue
		}
		wsm := b.unmarshal(bytes.NewReader(wsData))
		if b.err != nil {
			return fmt.Errorf("npm pack: failed to parse %s: %w", wsFile, b.err)

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Run the file through a JSON validator and fix the reported location.
  2. Compare against a known-good package.hugo.json (same shape as package.json: dependencies, devDependencies, workspaces).
  3. Delete package.hugo.json if you do not need Hugo-specific package merging (Hugo falls back to package.json).

Example fix

// before: package.hugo.json
{
  dependencies: { "jquery": "^3.0" } // unquoted key
}

// after
{
  "dependencies": { "jquery": "^3.0" }
}
Defensive patterns

Strategy: validation

Validate before calling

if data, err := os.ReadFile("package.hugo.json"); err == nil {
    var v any
    if err := json.Unmarshal(data, &v); err != nil {
        return fmt.Errorf("package.hugo.json invalid: %w", err)
    }
}

Prevention

When it happens

Trigger: A package.hugo.json file exists in the project root but contains invalid JSON.

Common situations: Hand-authoring package.hugo.json with syntax errors; partial git checkout; merging a theme's package.hugo.json incorrectly; converting from package.json and leaving invalid structure.

Understand the failure class

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/318395944fd396dc. Report an issue: GitHub.