gohugoio/hugo · error

npm pack: failed to parse package.json

Error message

npm pack: failed to parse package.json: %w

What it means

In npm.Pack (modules/npm/package_builder.go:73). The project-root package.json is read (afero.ReadFile succeeded) and handed to the packageBuilder.unmarshal, which runs json.Unmarshal. If that fails, b.err holds the JSON error and Pack returns it wrapped. This is the project-level package.json that Hugo uses to seed the autogenerated workspace package.

Solutions

  1. Validate package.json with a JSON linter or 'npm install' (npm will report the syntax error and location).
  2. Remove trailing commas, comments, and single quotes; ensure keys are double-quoted.
  3. If package.json is generated, regenerate it from source.

Example fix

// before
{
  "name": "proj",
  "scripts": { "dev": "vite", } // trailing comma
}

// after
{
  "name": "proj",
  "scripts": { "dev": "vite" }
}
Defensive patterns

Strategy: validation

Validate before calling

// validate package.json before invoking Hugo npm pack
data, err := os.ReadFile("package.json")
if err == nil {
    var v any
    if err := json.Unmarshal(data, &v); err != nil {
        return fmt.Errorf("package.json invalid: %w", err)
    }
}

Prevention

When it happens

Trigger: The root package.json exists but is not valid JSON (trailing comma, single quotes, unquoted keys, BOM, truncation, or a merge-conflict marker left in).

Common situations: Manual edits to package.json; a botched npm install / merge conflict; a file generated by another tool with non-strict JSON; copy-pasting JS-style comments into package.json.

Understand the failure class

Related errors


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

Appendix: source

Thrown at modules/npm/package_builder.go:73

		devDependencies:         make(map[string]any),
		devDependenciesComments: make(map[string]any),
		dependencies:            make(map[string]any),
		dependenciesComments:    make(map[string]any),
	}

	workspacePath := filepath.ToSlash(files.FolderPackagesHugoAutoGen)

	// skip modules that shouldn't have their package files processed, either because they are the project module (handled separately)
	// or because their UsePackageJSON setting disables it.
	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
	}

View on GitHub (pinned to 52c9bd7908)