Skip to content

Commit ea1cb87

Browse files
committed
content: Clarify modulule initializaton
Closes gohugoio#3398
1 parent 72315bc commit ea1cb87

File tree

3 files changed

+133
-75
lines changed

3 files changed

+133
-75
lines changed
Lines changed: 121 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,200 @@
11
---
22
title: Use Hugo Modules
3-
description: How to use Hugo Modules.
3+
description: Use modules to manage the content, layout, presentation, and behavior of your site.
44
categories: []
55
keywords: []
66
weight: 20
77
aliases: [/themes/usage/,/themes/installing/,/installing-and-using-themes/]
88
---
99

10-
## Prerequisite
10+
> [!note]
11+
> To work with modules you must install [Git][] and [Go][] 1.18 or later.
1112
12-
{{% include "/_common/gomodules-info.md" %}}
13+
## Introduction
1314

14-
## Initialize a new module
15+
{{% glossary-term module %}}
1516

16-
Use `hugo mod init` to initialize a new Hugo Module. If it fails to guess the module path, you must provide it as an argument, e.g.:
17+
- Modules can be imported in any combination or sequence.
18+
- Module imports are recursive; importing Module A can trigger the import of Module B, and so on.
19+
- Modules can provide configuration files and directories, subject to the constraints described in the [merge configuration settings][] section of the documentation.
20+
- External directories, including those from non-Hugo projects, can be mounted to create a [unified file system](g).
21+
22+
## Import
23+
24+
To import a module, first initialize the project itself as a module. For example:
1725

1826
```sh
19-
hugo mod init github.com/<your_user>/<your_project>
27+
hugo mod init github.com/user/project
2028
```
2129

22-
Also see the [CLI Doc](/commands/hugo_mod_init/).
30+
This will generate a [`go.mod`][] file in the project root.
31+
32+
> [!note]
33+
> The module name is a unique identifier rather than a hosting requirement. Using a name like `github.com/user/project` is a common convention but it does not mean you must use Git or host your code on GitHub. You can use any name you like if you do not plan to have others import your project as a module. For example, you could use a simple name such as `my-project` when you run the initialization command.
2334
24-
## Use a module for a theme
35+
Then define one or more imports in your site configuration. This contrived example imports three modules, each containing custom shortcodes:
2536

26-
The easiest way to use a Module for a theme is to import it in the configuration.
37+
{{< code-toggle file=hugo >}}
38+
[module]
39+
[[module.imports]]
40+
path = 'shortcodes-a'
41+
[[module.imports]]
42+
path = '/home/user/shortcodes-b'
43+
[[module.imports]]
44+
path = 'github.com/user/shortcodes-c'
45+
{{< /code-toggle >}}
2746

28-
1. Initialize the hugo module system: `hugo mod init github.com/<your_user>/<your_project>`
29-
1. Import the theme:
47+
Import precedence is top-down. For example, if `shortcodes-a`, `shortcodes-b`, and `shortcodes-c` each define an `image` shortcode, the `image` shortcode from `shortcodes-a` will take effect.
3048

31-
{{< code-toggle file=hugo >}}
32-
[module]
33-
[[module.imports]]
34-
path = "github.com/spf13/hyde"
35-
{{< /code-toggle >}}
49+
> [!note]
50+
> If multiple modules contain data files or [translation tables](g) with identical paths, the data is deeply merged, following top-down precedence.
3651
37-
## Update modules
52+
When you build your site, Hugo will:
3853

39-
Modules will be downloaded and added when you add them as imports to your configuration. See [configure modules](/configuration/module/#imports).
54+
1. Download the modules
55+
1. Cache them for future use
56+
1. Generate a [`go.sum`][] file in the project root
4057

41-
To update or manage versions, you can use `hugo mod get`.
58+
See [configuring module imports][] for details and options.
4259

43-
Some examples:
60+
## Update
4461

45-
### Update all modules
62+
When you import a module, Hugo creates `go.mod` and `go.sum` files in your project root, storing version and checksum data. Clearing the module cache and rebuilding will re-download the originally imported module version, as specified in the `go.mod` file, ensuring consistent builds. Modules can be updated to other versions as needed.
63+
64+
To update a module to the latest version:
4665

4766
```sh
48-
hugo mod get -u
67+
hugo mod get -u github.com/user/shortcodes-c
4968
```
5069

51-
### Update all modules recursively
70+
To update a module to a specific version:
5271

5372
```sh
54-
hugo mod get -u ./...
73+
hugo mod get -u github.com/user/shortcodes-c@v0.42.0
5574
```
5675

57-
### Update one module
76+
To update all modules to the latest version:
5877

5978
```sh
60-
hugo mod get -u github.com/gohugoio/myShortcodes
79+
hugo mod get -u
6180
```
6281

63-
### Get a specific version
82+
To recursively update all modules to the latest version:
6483

6584
```sh
66-
hugo mod get github.com/gohugoio/myShortcodes@v1.0.7
85+
hugo mod get -u ./...
6786
```
6887

69-
Also see the [CLI Doc](/commands/hugo_mod_get/).
70-
71-
## Make and test changes in a module
88+
## Tidy
7289

73-
One way to do local development of a module imported in a project is to add a replace directive to a local directory with the source in `go.mod`:
90+
To remove unused entries from the `go.mod` and `go.sum` files:
7491

7592
```sh
76-
replace github.com/bep/hugotestmods/mypartials => /Users/bep/hugotestmods/mypartials
93+
hugo mod tidy
7794
```
7895

79-
If you have the `hugo server` running, the configuration will be reloaded and `/Users/bep/hugotestmods/mypartials` put on the watch list.
96+
## Cache
8097

81-
Instead of modifying the `go.mod` files, you can also use the modules configuration [`replacements`](/configuration/module/#top-level-options) option.
98+
Hugo caches modules to avoid repeated downloads during site builds. By default, these are stored in the `modules` directory within the [`cacheDir`][].
8299

83-
## Print dependency graph
100+
To clean the module cache for the current project:
84101

85-
Use `hugo mod graph` from the relevant module directory and it will print the dependency graph, including vendoring, module replacement or disabled status.
86-
87-
E.g.:
102+
```sh
103+
hugo mod clean
104+
```
88105

89-
```txt
90-
hugo mod graph
106+
To clean the module cache for all projects:
91107

92-
github.com/bep/my-modular-site github.com/bep/hugotestmods/mymounts@v1.2.0
93-
github.com/bep/my-modular-site github.com/bep/hugotestmods/mypartials@v1.0.7
94-
github.com/bep/hugotestmods/mypartials@v1.0.7 github.com/bep/hugotestmods/myassets@v1.0.4
95-
github.com/bep/hugotestmods/mypartials@v1.0.7 github.com/bep/hugotestmods/myv2@v1.0.0
96-
DISABLED github.com/bep/my-modular-site github.com/spf13/hyde@v0.0.0-20190427180251-e36f5799b396
97-
github.com/bep/my-modular-site github.com/bep/hugo-fresh@v1.0.1
98-
github.com/bep/my-modular-site in-themesdir
108+
```sh
109+
hugo mod clean --all
99110
```
100111

101-
Also see the [CLI Doc](/commands/hugo_mod_graph/).
112+
For details on cache location and eviction, see [configuring file caches][].
102113

103-
## Vendor your modules
114+
## Vendor
104115

105-
`hugo mod vendor` will write all the module dependencies to a `_vendor` directory, which will then be used for all subsequent builds.
116+
{{% glossary-term vendor %}}
106117

107-
Note that:
118+
Vendoring a module provides the benefits described above and allows for local inspection of its [components](g).
108119

109-
- You can run `hugo mod vendor` on any level in the module tree.
110-
- Vendoring will not store modules stored in your `themes` directory.
111-
- Most commands accept a `--ignoreVendorPaths` flag, which will then not use the vendored modules in `_vendor` for the module paths matching the given [glob pattern](g).
112-
113-
Also see the [CLI Doc](/commands/hugo_mod_vendor/).
120+
```sh
121+
hugo mod vendor
122+
```
114123

115-
## Tidy go.mod, go.sum
124+
This command creates a `_vendor` directory containing copies of all imported modules, used in subsequent builds. Note that:
116125

117-
Run `hugo mod tidy` to remove unused entries in `go.mod` and `go.sum`.
126+
- The `hugo mod vendor` command can be run from any module tree level.
127+
- Modules within the `themes` directory are not vendored.
128+
- The `--ignoreVendorPaths` flag allows you to exclude vendored modules matching a [glob pattern](g) from specific commands.
118129

119-
Also see the [CLI Doc](/commands/hugo_mod_clean/).
130+
> [!important]
131+
> Instead of modifying files directly within the `_vendor` directory, override them by creating a corresponding file with the same relative path in your project's root.
120132
121-
## Clean module cache
133+
To remove the vendored modules, delete the `_vendor` directory.
122134

123-
Run `hugo mod clean` to delete the entire modules cache.
135+
## Replace
124136

125-
Note that you can also configure the `modules` cache with a `maxAge`. See [configure caches](/configuration/caches/).
137+
For local module development, use a `replace` directive in `go.mod` pointing to your local directory:
126138

127-
Also see the [CLI Doc](/commands/hugo_mod_clean/).
139+
```text
140+
replace github.com/user/module => /home/user/projects/module
141+
```
128142

129-
## Module workspaces
143+
With `hugo serve`r running, this change will trigger a configuration reload and add the local directory to the watch list. Alternatively, configure replacements by setting the [`replacements`][] parameter in your site configuration.
130144

131-
Workspace support was added in [Go 1.18](https://go.dev/blog/get-familiar-with-workspaces) and Hugo got solid support for it in the `v0.109.0` version.
145+
## Workspace
132146

133-
A common use case for a workspace is to simplify local development of a site with its theme modules.
147+
{{% glossary-term "workspace" %}}
134148

135-
A workspace can be configured in a `*.work` file and activated with the [module.workspace](/configuration/module/) setting, which for this use is commonly controlled via the `HUGO_MODULE_WORKSPACE` OS environment variable.
149+
Workspaces simplify local development of sites with modules. Create a `.work` file to define a workspace, and activate it via the [`workspace`][] configuration parameter or the `HUGO_MODULE_WORKSPACE` environment variable.
136150

137-
See the [hugo.work](https://github.com/gohugoio/hugo/blob/master/docs/hugo.work) file in the Hugo Docs repo for an example:
151+
A `.work` file example:
138152

139153
```text
140-
go 1.20
154+
go 1.24
141155
142156
use .
143-
use ../gohugoioTheme
157+
use ../my-hugo-module
144158
```
145159

146-
Using the `use` directive, list all the modules you want to work on, pointing to its relative location. As in the example above, it's recommended to always include the main project (the `.`) in the list.
147-
148-
With that you can start the Hugo server with that workspace enabled:
160+
Use the `use` directive to list module paths, including the main project (`.`). Start the Hugo server with the workspace enabled:
149161

150162
```sh
151163
HUGO_MODULE_WORKSPACE=hugo.work hugo server --ignoreVendorPaths "**"
152164
```
153165

154-
The `--ignoreVendorPaths` flag is added above to ignore any of the vendored dependencies inside `_vendor`. If you don't use vendoring, you don't need that flag. But now the server is set up watching the files and directories in the workspace and you can see your local edits reloaded.
166+
The `--ignoreVendorPaths` flag, used to ignore vendored dependencies (if applicable), enables live reloading of local edits within the workspace.
167+
168+
## Graph
169+
170+
To generate a [dependency graph](g), including vendoring, module replacement, and disabled module information, execute `hugo mod graph` within the target module directory. For example:
171+
172+
```sh
173+
$ hugo mod graph
174+
175+
github.com/bep/my-modular-site github.com/bep/hugotestmods/mymounts@v1.2.0
176+
github.com/bep/my-modular-site github.com/bep/hugotestmods/mypartials@v1.0.7
177+
github.com/bep/hugotestmods/mypartials@v1.0.7 github.com/bep/hugotestmods/myassets@v1.0.4
178+
github.com/bep/hugotestmods/mypartials@v1.0.7 github.com/bep/hugotestmods/myv2@v1.0.0
179+
DISABLED github.com/bep/my-modular-site github.com/spf13/hyde@v0.0.0-20190427180251-e36f5799b396
180+
github.com/bep/my-modular-site github.com/bep/hugo-fresh@v1.0.1
181+
github.com/bep/my-modular-site in-themesdir
182+
```
183+
184+
## Mounts
185+
186+
Imported modules automatically mount their component directories to Hugo's [unified file system](g). You can also manually mount any directory, including those from non-Hugo projects, to component directories.
187+
188+
See [configuring module mounts][] for details.
189+
190+
[`cacheDir`]: /configuration/all/#cachedir
191+
[`go.mod`]: https://go.dev/ref/mod#go-mod-file
192+
[`go.sum`]: https://go.dev/ref/mod#go-sum-files
193+
[`replacements`]: /configuration/module/#replacements
194+
[`workspace`]: /configuration/module/#workspace
195+
[configuring file caches]: /configuration/caches/
196+
[configuring module imports]: /configuration/module/#imports
197+
[configuring module mounts]: /configuration/module/#mounts
198+
[Git]: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
199+
[Go]: https://go.dev/doc/install
200+
[merge configuration settings]: /configuration/introduction/#merge-configuration-settings
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: dependency graph
3+
---
4+
5+
A _dependency graph_ visually represents the relationships between the [_modules_](g) used in a Hugo project. It shows how modules depend on each other, forming a network of dependencies.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: workspace
3+
params:
4+
reference: /hugo-modules/use-modules/#workspace
5+
---
6+
7+
A _workspace_ is a collection of [_modules_](g) on disk.

0 commit comments

Comments
 (0)