|
1 | 1 | --- |
2 | 2 | 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. |
4 | 4 | categories: [] |
5 | 5 | keywords: [] |
6 | 6 | weight: 20 |
7 | 7 | aliases: [/themes/usage/,/themes/installing/,/installing-and-using-themes/] |
8 | 8 | --- |
9 | 9 |
|
10 | | -## Prerequisite |
| 10 | +> [!note] |
| 11 | +> To work with modules you must install [Git][] and [Go][] 1.18 or later. |
11 | 12 |
|
12 | | -{{% include "/_common/gomodules-info.md" %}} |
| 13 | +## Introduction |
13 | 14 |
|
14 | | -## Initialize a new module |
| 15 | +{{% glossary-term module %}} |
15 | 16 |
|
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: |
17 | 25 |
|
18 | 26 | ```sh |
19 | | -hugo mod init github.com/<your_user>/<your_project> |
| 27 | +hugo mod init github.com/user/project |
20 | 28 | ``` |
21 | 29 |
|
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. |
23 | 34 |
|
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: |
25 | 36 |
|
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 >}} |
27 | 46 |
|
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. |
30 | 48 |
|
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. |
36 | 51 |
|
37 | | -## Update modules |
| 52 | +When you build your site, Hugo will: |
38 | 53 |
|
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 |
40 | 57 |
|
41 | | -To update or manage versions, you can use `hugo mod get`. |
| 58 | +See [configuring module imports][] for details and options. |
42 | 59 |
|
43 | | -Some examples: |
| 60 | +## Update |
44 | 61 |
|
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: |
46 | 65 |
|
47 | 66 | ```sh |
48 | | -hugo mod get -u |
| 67 | +hugo mod get -u github.com/user/shortcodes-c |
49 | 68 | ``` |
50 | 69 |
|
51 | | -### Update all modules recursively |
| 70 | +To update a module to a specific version: |
52 | 71 |
|
53 | 72 | ```sh |
54 | | -hugo mod get -u ./... |
| 73 | +hugo mod get -u github.com/user/shortcodes-c@v0.42.0 |
55 | 74 | ``` |
56 | 75 |
|
57 | | -### Update one module |
| 76 | +To update all modules to the latest version: |
58 | 77 |
|
59 | 78 | ```sh |
60 | | -hugo mod get -u github.com/gohugoio/myShortcodes |
| 79 | +hugo mod get -u |
61 | 80 | ``` |
62 | 81 |
|
63 | | -### Get a specific version |
| 82 | +To recursively update all modules to the latest version: |
64 | 83 |
|
65 | 84 | ```sh |
66 | | -hugo mod get github.com/gohugoio/myShortcodes@v1.0.7 |
| 85 | +hugo mod get -u ./... |
67 | 86 | ``` |
68 | 87 |
|
69 | | -Also see the [CLI Doc](/commands/hugo_mod_get/). |
70 | | - |
71 | | -## Make and test changes in a module |
| 88 | +## Tidy |
72 | 89 |
|
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: |
74 | 91 |
|
75 | 92 | ```sh |
76 | | -replace github.com/bep/hugotestmods/mypartials => /Users/bep/hugotestmods/mypartials |
| 93 | +hugo mod tidy |
77 | 94 | ``` |
78 | 95 |
|
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 |
80 | 97 |
|
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`][]. |
82 | 99 |
|
83 | | -## Print dependency graph |
| 100 | +To clean the module cache for the current project: |
84 | 101 |
|
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 | +``` |
88 | 105 |
|
89 | | -```txt |
90 | | -hugo mod graph |
| 106 | +To clean the module cache for all projects: |
91 | 107 |
|
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 |
99 | 110 | ``` |
100 | 111 |
|
101 | | -Also see the [CLI Doc](/commands/hugo_mod_graph/). |
| 112 | +For details on cache location and eviction, see [configuring file caches][]. |
102 | 113 |
|
103 | | -## Vendor your modules |
| 114 | +## Vendor |
104 | 115 |
|
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 %}} |
106 | 117 |
|
107 | | -Note that: |
| 118 | +Vendoring a module provides the benefits described above and allows for local inspection of its [components](g). |
108 | 119 |
|
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 | +``` |
114 | 123 |
|
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: |
116 | 125 |
|
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. |
118 | 129 |
|
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. |
120 | 132 |
|
121 | | -## Clean module cache |
| 133 | +To remove the vendored modules, delete the `_vendor` directory. |
122 | 134 |
|
123 | | -Run `hugo mod clean` to delete the entire modules cache. |
| 135 | +## Replace |
124 | 136 |
|
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: |
126 | 138 |
|
127 | | -Also see the [CLI Doc](/commands/hugo_mod_clean/). |
| 139 | +```text |
| 140 | +replace github.com/user/module => /home/user/projects/module |
| 141 | +``` |
128 | 142 |
|
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. |
130 | 144 |
|
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 |
132 | 146 |
|
133 | | -A common use case for a workspace is to simplify local development of a site with its theme modules. |
| 147 | +{{% glossary-term "workspace" %}} |
134 | 148 |
|
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. |
136 | 150 |
|
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: |
138 | 152 |
|
139 | 153 | ```text |
140 | | -go 1.20 |
| 154 | +go 1.24 |
141 | 155 |
|
142 | 156 | use . |
143 | | -use ../gohugoioTheme |
| 157 | +use ../my-hugo-module |
144 | 158 | ``` |
145 | 159 |
|
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: |
149 | 161 |
|
150 | 162 | ```sh |
151 | 163 | HUGO_MODULE_WORKSPACE=hugo.work hugo server --ignoreVendorPaths "**" |
152 | 164 | ``` |
153 | 165 |
|
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 |
0 commit comments