Debian Codegraph @

docs/content/en/hugo-modules/use-modules.md

raw
1 ---
2 title: Use modules
3 description: Use modules to manage the content, layout, presentation, and behavior of your site.
4 categories: []
5 keywords: []
6 weight: 20
7 aliases: [/themes/usage/,/themes/installing/,/installing-and-using-themes/]
8 ---
9
10 > [!NOTE]
11 > To work with modules you must install [Git][] and [Go][] 1.18 or later.
12
13 ## Introduction
14
15 {{% glossary-term module %}}
16
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:
25
26 ```sh
27 hugo mod init github.com/user/project
28 ```
29
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.
34
35 Then define one or more imports in your project configuration. This contrived example imports three modules, each containing custom shortcodes:
36
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 >}}
46
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.
48
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.
51
52 When you build your project, Hugo will:
53
54 1. Download the modules
55 1. Cache them for future use
56 1. Generate a [`go.sum`][] file in the project root
57
58 See [configuring module imports][] for details and options.
59
60 ## Update
61
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:
65
66 ```sh
67 hugo mod get -u github.com/user/shortcodes-c
68 ```
69
70 To update a module to a specific version:
71
72 ```sh
73 hugo mod get -u github.com/user/[email protected]
74 ```
75
76 To update all modules to the latest version:
77
78 ```sh
79 hugo mod get -u
80 ```
81
82 To recursively update all modules to the latest version:
83
84 ```sh
85 hugo mod get -u ./...
86 ```
87
88 ## Tidy
89
90 To remove unused entries from the `go.mod` and `go.sum` files:
91
92 ```sh
93 hugo mod tidy
94 ```
95
96 ## Cache
97
98 Hugo caches modules to avoid repeated downloads during site builds. By default, these are stored in the `modules` directory within the [`cacheDir`][].
99
100 To clean the module cache for the current project:
101
102 ```sh
103 hugo mod clean
104 ```
105
106 To clean the module cache for all projects:
107
108 ```sh
109 hugo mod clean --all
110 ```
111
112 For details on cache location and eviction, see [configuring file caches][].
113
114 ## Vendor
115
116 {{% glossary-term vendor %}}
117
118 Vendoring a module provides the benefits described above and allows for local inspection of its [components](g).
119
120 ```sh
121 hugo mod vendor
122 ```
123
124 This command creates a `_vendor` directory containing copies of all imported modules, used in subsequent builds. Note that:
125
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.
129
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.
132
133 To remove the vendored modules, delete the `_vendor` directory.
134
135 ## Replace
136
137 For local module development, use a `replace` directive in `go.mod` pointing to your local directory:
138
139 ```text
140 replace github.com/user/module => /home/user/projects/module
141 ```
142
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 project configuration.
144
145 ## Workspace
146
147 {{% glossary-term "workspace" %}}
148
149 Workspaces simplify local development of sites with modules. Create a `.work` file to define a workspace, and activate it via the [`workspace`][] configuration setting or the `HUGO_MODULE_WORKSPACE` environment variable.
150
151 A `.work` file example:
152
153 ```text
154 go 1.26
155
156 use .
157 use ../my-hugo-module
158 ```
159
160 Use the `use` directive to list module paths, including the main project (`.`). Start the Hugo server with the workspace enabled:
161
162 ```sh
163 HUGO_MODULE_WORKSPACE=hugo.work hugo server --ignoreVendorPaths "**"
164 ```
165
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/[email protected]
176 github.com/bep/my-modular-site github.com/bep/hugotestmods/[email protected]
177 github.com/bep/hugotestmods/[email protected] github.com/bep/hugotestmods/[email protected]
178 github.com/bep/hugotestmods/[email protected] github.com/bep/hugotestmods/[email protected]
179 DISABLED github.com/bep/my-modular-site github.com/spf13/[email protected]
180 github.com/bep/my-modular-site github.com/bep/[email protected]
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 [Git]: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
191 [Go]: https://go.dev/doc/install
192 [`cacheDir`]: /configuration/all/#cachedir
193 [`go.mod`]: https://go.dev/ref/mod#go-mod-file
194 [`go.sum`]: https://go.dev/ref/mod#go-sum-files
195 [`replacements`]: /configuration/module/#replacements
196 [`workspace`]: /configuration/module/#workspace
197 [configuring file caches]: /configuration/caches/
198 [configuring module imports]: /configuration/module/#imports
199 [configuring module mounts]: /configuration/module/#mounts
200 [merge configuration settings]: /configuration/introduction/#merge-configuration-settings