This guide explains how to create and manage Hugo modules, including the structure, configuration, and best practices for module development.
This guide explains how to create and manage Hugo modules, including the structure, configuration, and best practices for module development. Learn how to package layouts, assets, and shortcodes into reusable modules that can be shared across multiple Hugo sites.
Reusable Hugo modules allow you to package layouts, assets, shortcodes, and configuration logic into a versioned, composable unit. This step walks you through initializing your module, structuring it correctly, and preparing it for use in other Hugo sites.
Before you begin, ensure you have:
Create a new folder for your module, ideally named after the module itself:
1mkdir ags-mcq && cd ags-mcq
Note
The name ags-mcq is chosen for this example, the first part is to represent the
vendorwhile second part is thenameof the module. You can choose any name that follows the Go module naming conventions.
Inside this folder, youβll eventually include:
1ags-mcq/
2βββ go.mod # Required: declares module path
3βββ layouts/
4β βββ shortcodes/ # Shortcodes used by consuming sites
5βββ assets/
6β βββ hb/modules/agsayyed/ # Custom assets (e.g., JS, CSS, images)
7βββ static/
8β βββ sounds/ # Static files (e.g., audio clips)
9βββ data/ # Optional: structured data files
10βββ README.md # Strongly recommended: usage guide
11βββ config.toml # Required: default params and mounts
This structure ensures your module is composable and override-friendly when imported into a Hugo site.
Run the following command inside your module directory:
1hugo mod init github.com/agsayyed/ags-ags-mcq
This creates a go.mod file like:
1module github.com/agsayyed/ags-ags-mcq
2
3go 1.23.2
layouts/shortcodes/: Shortcodes are the primary interface for reusable logic. Placing them here makes them discoverable by Hugo.assets/: For SCSS, JS, or image pipelines. Hugo processes these via its asset pipeline.static/: For files served directly (e.g., audio clips used in ags-mcqs).go.mod: Required for module resolution and versioning.README.md: Helps consumers understand how to use your module.config.toml: Contains default parameters and mounts for the module.Once your module is initialized and structured, the next step is to define its configuration. This includes declaring dependencies (other modules), mounting directories, and setting default parameters that consuming sites can override.
The config.toml file is crucial for defining default parameters and mounts for your module. Hereβs an example:
1# config.toml
2
3[module]
4hugoVersion = { extended = true, min = "0.110.0" }
5
6[[module.imports]]
7path = ""
8
9[[module.mounts]]
10source = "layouts"
11target = "layouts"
12
13[[module.mounts]]
14source = "assets/hb/modules/agsayyed"
15target = "assets/hb/modules/agsayyed"
16
17
18[params.hb]
19bootstrap.enable_library = true
20
21[params.ags-mcq]
22enable = true
23soundPath = "sounds/"
[module]
[[module.imports]]
go.mod when your module is imported.[[module.mounts]]
[params]
content/ unless you’re shipping starter content.params.ags-mcq) to avoid collisions.