gohugoio/hugo · error

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

Error message

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

What it means

During `hugo mod npm pack`, Hugo reads the project's root package.json (step 1 of Pack, and again in ensureWorkspaceRef) to merge dependencies and register its autogenerated workspace. If the file exists but is not valid JSON, unmarshalling fails and packing aborts with this wrapped parse error.

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 8a468df065)

Solutions

  1. Validate the root package.json with `jq . package.json` (or `node -e "JSON.parse(require('fs').readFileSync('package.json'))"`) and fix the reported syntax error.
  2. Remove comments/trailing commas — package.json must be strict JSON.
  3. Check for and resolve git merge-conflict markers (`<<<<<<<`) in the file.
  4. If the file is beyond repair, regenerate it (`npm init -y`) and re-add your dependencies, then re-run `hugo mod npm pack`.

Example fix

// before (package.json)
{
  "dependencies": {
    "tailwindcss": "^3.4.0", // css framework
  },
}
// after
{
  "dependencies": {
    "tailwindcss": "^3.4.0"
  }
}
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

if err := npm.Pack(fs, deps); err != nil {
    var jsonErr *json.SyntaxError
    if errors.As(err, &jsonErr) {
        log.Fatalf("fix package.json syntax near offset %d: %v", jsonErr.Offset, err)
    }
    return err
}

Prevention

When it happens

Trigger: Running `hugo mod npm pack` (or a build path that triggers npm packing) with a root package.json containing invalid JSON — trailing commas, comments, unquoted keys, merge-conflict markers, or truncated content.

Common situations: Hand-edited package.json with a trailing comma or `//` comment (JSON5 habits); unresolved git merge conflict markers; a file corrupted by a partial write or wrong encoding (UTF-16/BOM issues).

Related errors


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