gohugoio/hugo · error

failed to load data: %w

Error message

failed to load data: %w

What it means

Returned by HugoSites.Data() in hugolib/hugo_sites.go when the lazy, once-per-build initializer that walks and parses all /data directories fails. Accessing `site.Data` (or .Site.Data in templates) triggers this load; any unreadable or unparseable data file aborts it, and the error is sent to the site's fatal error handler.

Source

Thrown at hugolib/hugo_sites.go:339

	defer f.mu.Unlock()
	return f.err
}

func (f *fatalErrorHandler) Done() <-chan bool {
	return f.donec
}

type hugoSitesInit struct {
	// Loads the data from all of the /data folders.
	data hsync.FuncResetter

	// Loads the Git info and CODEOWNERS for all the pages if enabled.
	gitInfo hsync.FuncResetter
}

func (h *HugoSites) Data() map[string]any {
	if err := h.init.data.Do(context.Background()); err != nil {
		h.SendError(fmt.Errorf("failed to load data: %w", err))
		return nil
	}
	return h.data
}

// Pages returns all pages for all sites.
func (h *HugoSites) Pages() page.Pages {
	key := "pages"
	v, err := h.cachePages.GetOrCreate(key, func(string) (page.Pages, error) {
		var pages page.Pages
		for _, s := range h.Sites {
			pages = append(pages, s.Pages()...)
		}
		page.SortByDefault(pages)
		return pages, nil
	})
	if err != nil {
		panic(err)

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Read the wrapped error — it names the offending data file and parse position; fix the syntax there.
  2. Validate the file with an external linter (yamllint, jq, taplo) matching its extension.
  3. Ensure every file under data/ has an extension matching its actual format (json/yaml/toml/xml/csv).
  4. Check module/theme mounts in hugo.toml [[module.mounts]] aren't mounting stray files into data/.

Example fix

# data/authors.yaml before (tab indentation — invalid YAML)
authors:
	- name: Jo
# after (spaces)
authors:
  - name: Jo
Defensive patterns

Strategy: try-catch

Validate before calling

# validate data files before build
find data -name '*.json' -exec jq empty {} \;
find data -name '*.yaml' -o -name '*.yml' | xargs -I{} yq e '.' {} > /dev/null

Try / catch

if err := h.Build(cfg); err != nil {
    if strings.Contains(err.Error(), "failed to load data") {
        // a file under data/ is malformed — the wrapped error names it
    }
    return err
}

Prevention

When it happens

Trigger: First template access to `.Site.Data` (or hugo build touching data) when any file under data/ — in the project, a theme, or a mounted module — fails to open or parse: invalid JSON/YAML/TOML/XML/CSV, unsupported extension handling, or filesystem permission errors.

Common situations: Malformed YAML (tabs, unquoted colons) in data/*.yaml; JSON with trailing commas; a data file saved with the wrong extension for its syntax; module mounts pointing data/ at a directory containing non-data files; merge conflicts committed into data files.

Related errors


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