<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Hugo Modules on
A Scripter's Notes</title><link>https://scripter.co/series/hugo-modules/</link><description>Recent content in Hugo Modules
on A Scripter's Notes</description><language>en-us</language><managingEditor>kaushal.modi@gmail.com (Kaushal Modi)</managingEditor><webMaster>kaushal.modi@gmail.com (Kaushal Modi)</webMaster><lastBuildDate>Wed, 22 Apr 2026 08:24:58 -0400</lastBuildDate><generator>Hugo -- gohugo.io</generator><docs>https://validator.w3.org/feed/docs/rss2.html</docs><atom:link href="https://scripter.co/series/hugo-modules/index.xml" rel="self" type="application/rss+xml"/><item><title>Hugo Modules: Importing a Theme</title><link>https://scripter.co/hugo-modules-importing-a-theme/</link><description>&lt;blockquote>A brief guide on how to install Hugo themes using Hugo Modules.&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#clean-up-the-old-way-of-setting-a-theme">&lt;span class="section-num">1&lt;/span> Clean up the old way of setting a theme&lt;/a>&lt;/li>
&lt;li>&lt;a href="#import-the-theme-module">&lt;span class="section-num">2&lt;/span> Import the &amp;ldquo;theme&amp;rdquo; module&lt;/a>
&lt;ul>
&lt;li>&lt;a href="#module-imports-example">Quick Example&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;a href="#hugo-mod-tidy">&lt;span class="section-num">3&lt;/span> &lt;code>hugo mod tidy&lt;/code>&lt;/a>&lt;/li>
&lt;li>&lt;a href="#updating-the-theme">&lt;span class="section-num">4&lt;/span> Updating the theme&lt;/a>&lt;/li>
&lt;li>&lt;a href="#dependency-graph">Dependency Graph&lt;/a>&lt;/li>
&lt;li>&lt;a href="#building-your-hugo-site-on-a-server">Building your Hugo site on a server&lt;/a>&lt;/li>
&lt;li>&lt;a href="#in-a-nutshell">In a nutshell&lt;/a>&lt;/li>
&lt;li>&lt;a href="#references">References&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;p>Hello! You are reading this post because you are probably interested
in the Hugo Modules feature and are considering to import a Hugo
Module as a theme.&lt;/p>
&lt;p>&lt;strong>Step 0&lt;/strong> for that approach is to make your site repo a Hugo Module. If
your site already is, then it would have a &lt;code>go.mod&lt;/code> file in the repo
root. If you don&amp;rsquo;t have the &lt;code>go.mod&lt;/code> file, check out the previous post
&lt;a href="https://scripter.co/hugo-modules-getting-started/">Hugo Modules: Getting Started&lt;/a> first ‼️&lt;/p>
&lt;div class="note">
&lt;p>If you don&amp;rsquo;t have a &lt;code>go.mod&lt;/code> file for your site repo, and you still
decide to continue with the next steps, don&amp;rsquo;t complain if you see
errors like &lt;em>module &amp;ldquo;foo&amp;rdquo; not found; either add it as a Hugo Module or
store it in &amp;ldquo;&amp;lt;your site repo&amp;gt;/themes&amp;rdquo;.: module does not exist&lt;/em>. &amp;mdash;
&lt;a href="https://discourse.gohugo.io/t/hugo-mod-init-fails-to-create-go-mod-if-hugo-detects-an-error-prematurely-in-site-config-toml/36687">speaking from experience&lt;/a> 😉.&lt;/p>
&lt;/div>
&lt;p>With that out of the way, here are the next steps ..&lt;/p>
&lt;h2 id="clean-up-the-old-way-of-setting-a-theme">&lt;span class="section-num">1&lt;/span> Clean up the old way of setting a theme&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#clean-up-the-old-way-of-setting-a-theme">#&lt;/a>&lt;/h2>
&lt;p>If you are upgrading your Hugo site to switch from the legacy method
of using themes (i.e using the &lt;code>theme&lt;/code> variable in the site config
&lt;span class="sidenote-number">&lt;small class="sidenote">
In my posts, you may have seen me use the &lt;em>Site Config&lt;/em> term or
&lt;code>config.toml&lt;/code> &amp;ndash; They mean the same thing.
&lt;/small>&lt;/span>
), you need to clean that up.&lt;/p>
&lt;ol>
&lt;li>Remove the &lt;code>theme&lt;/code> variable from your site config.&lt;/li>
&lt;li>Remove the &lt;code>themes&lt;/code> directory, or move it out of your Hugo site
repo.
&lt;ul>
&lt;li>If you were cloning a theme developed by someone else in there,
you can just remove this directory.&lt;/li>
&lt;li>If you are maintaining your own theme in that directory, move it
out of your site repo and &lt;a href="https://scripter.co/hugo-modules-getting-started/#convert-to-hugo-module">convert it to a Hugo Module&lt;/a>.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ol>
&lt;h2 id="import-the-theme-module">&lt;span class="section-num">2&lt;/span> Import the &amp;ldquo;theme&amp;rdquo; module&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#import-the-theme-module">#&lt;/a>&lt;/h2>
&lt;p>The &lt;em>theme&lt;/em> is quoted in this title, because the concept of a Hugo
&amp;ldquo;theme&amp;rdquo; is a bit old now (&lt;span class="timestamp-wrapper">&lt;span class="timestamp">&amp;lt;2022-05-26 Thu&amp;gt;&lt;/span>&lt;/span>) and that has been
superseded with the concept of &amp;ldquo;modules&amp;rdquo;.&lt;/p>
&lt;p>The main difference between a theme and a generic &lt;em>Hugo Module&lt;/em> is
that the former will allow you to build your site entirely, while the
latter might implement only some modular features like enabling the
ATOM feed, or adding a search to your website.&lt;/p>
&lt;div class="note">
&lt;p>I am mentioning this again for convenience, from &lt;a href="https://scripter.co/hugo-modules-getting-started/">the previous post in
this series&lt;/a>:&lt;/p>
&lt;p>&lt;em>A module can be your main project or a smaller module providing one
or more of the 7 component types defined in Hugo: &lt;strong>static&lt;/strong>, &lt;strong>content&lt;/strong>,
&lt;strong>layouts&lt;/strong>, &lt;strong>data&lt;/strong>, &lt;strong>assets&lt;/strong>, &lt;strong>i18n&lt;/strong>, and &lt;strong>archetypes&lt;/strong>. You can combine
modules in any combination you like, and even mount directories from
non-Hugo projects, forming a big, virtual union file system.&lt;/em>&lt;/p>
&lt;/div>
&lt;p>A theme will need to have the &amp;ldquo;layout&amp;rdquo; component. Additionally, it
might have the &amp;ldquo;assets&amp;rdquo;, &amp;ldquo;static&amp;rdquo;, and other components too.&lt;/p>
&lt;p>Importing a module as a theme will typically look like this in your
site config:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-toml" data-lang="toml">&lt;span class="line">&lt;span class="cl">&lt;span class="p">[&lt;/span>&lt;span class="nx">module&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">[[&lt;/span>&lt;span class="nx">module&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">imports&lt;/span>&lt;span class="p">]]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">path&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s2">&amp;#34;URL of the theme&amp;#39;s git remote *without* the &amp;#39;https://&amp;#39; part&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;strong>path&lt;/strong> here would be something like
&lt;code>github.com/USER/THEME-REPO-NAME&lt;/code> or
&lt;code>gitlab.com/USER/THEME-REPO-NAME&lt;/code>.&lt;/p>
&lt;dl>
&lt;dt>Note&lt;/dt>
&lt;dd>It&amp;rsquo;s possible to take &lt;span class="underline">any&lt;/span> Hugo theme git repo and import
that as a Hugo Module even if that repo isn&amp;rsquo;t actually one
i.e. doesn&amp;rsquo;t have a &lt;code>go.mod&lt;/code>. But it&amp;rsquo;s recommended that the theme be
a proper Hugo Module so that you have better dependency tracking
between your site and the theme.&lt;/dd>
&lt;/dl>
&lt;h3 id="module-imports-example">Quick Example&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#module-imports-example">#&lt;/a>&lt;/h3>
&lt;p>Follow these steps if you want to try out how this Hugo Module based
theme importing&lt;/p>
&lt;div class="note">
&lt;p>As a reminder, you &lt;a href="https://scripter.co/hugo-modules-getting-started/#install-a-recent-version-of-go">need to have Go installed&lt;/a>
on your system.&lt;/p>
&lt;/div>
&lt;ol>
&lt;li>Create a temporary directory somewhere and &lt;code>cd&lt;/code> to it.&lt;/li>
&lt;li>Initialize your site as a Hugo Module: &lt;code>hugo mod init foo&lt;/code> (yeah,
type that out literally &amp;mdash; it will work)&lt;/li>
&lt;li>Create a &lt;code>config.toml&lt;/code> file with the below content. It imports the
&lt;a href="https://gitlab.com/kaushalmodi/hugo-mwe-theme">&lt;code>hugo-mwe-theme&lt;/code>&lt;/a>
&lt;span class="sidenote-number">&lt;small class="sidenote">
&lt;code>hugo-mwe-theme&lt;/code> is a minimal Hugo theme that I use to quickly try
out some new feature in Hugo or to create a &lt;em>minimal working
example&lt;/em> to reproduce a bug.
&lt;/small>&lt;/span>
theme.
&lt;a id="code-snippet--theme-module-import-example">&lt;/a>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-toml" data-lang="toml">&lt;span class="line">&lt;span class="cl">&lt;span class="p">[&lt;/span>&lt;span class="nx">module&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">[[&lt;/span>&lt;span class="nx">module&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">imports&lt;/span>&lt;span class="p">]]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">path&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="s2">&amp;#34;gitlab.com/kaushalmodi/hugo-mwe-theme&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="src-block-caption">
&lt;span class="src-block-number">&lt;a href="#code-snippet--theme-module-import-example">Code Snippet 1&lt;/a>:&lt;/span>
Example of importing a Hugo module as a theme in &lt;code>config.toml&lt;/code>
&lt;/div>
&lt;/li>
&lt;li>Create &lt;code>content/hello.md&lt;/code>. This step is optional and is only so
that your test site as some content.
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-md" data-lang="md">&lt;span class="line">&lt;span class="cl">+++
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">title = &amp;#34;Hello&amp;#34;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">+++
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Hey!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ol>
&lt;p>That&amp;rsquo;s it! Now run the Hugo server (&lt;code>hugo server&lt;/code>) and look at your
site running on localhost .. while thinking in disbelief.. &lt;em>just how
easy all of this was!&lt;/em> 😃.&lt;/p>
&lt;ul>
&lt;li>Did you need to manually clone any theme? &lt;strong>No&lt;/strong>&lt;/li>
&lt;li>Would you need to deal with the &lt;code>.gitmodules&lt;/code> file? &lt;strong>No&lt;/strong>&lt;/li>
&lt;/ul>
&lt;h2 id="hugo-mod-tidy">&lt;span class="section-num">3&lt;/span> &lt;code>hugo mod tidy&lt;/code>&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#hugo-mod-tidy">#&lt;/a>&lt;/h2>
&lt;p>Finally, run &lt;a href="https://gohugo.io/commands/hugo_mod_tidy/">&lt;code>hugo mod tidy&lt;/code>&lt;/a> to clean up the &lt;code>go.mod&lt;/code> and
update/generate the &lt;code>go.sum&lt;/code> file. These files will track the module
dependencies for your site.&lt;/p>
&lt;ul>
&lt;li>The &lt;code>go.mod&lt;/code> contains the direct module dependencies for your site.&lt;/li>
&lt;li>The &lt;code>go.sum&lt;/code> contains the versions and hashes of all the direct &lt;strong>and
indirect&lt;/strong> dependencies
&lt;span class="sidenote-number">&lt;small class="sidenote">
Just as you added a theme as a Hugo Module to your site, it&amp;rsquo;s
possible that that theme is depending on other Hugo Modules (like
the ones I mentioned earlier: ATOM feeds, search, etc.).
&lt;/small>&lt;/span>
for your site.&lt;/li>
&lt;/ul>
&lt;div class="note">
&lt;p>You would need to commit the &lt;code>go.mod&lt;/code> and &lt;code>go.sum&lt;/code>&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> files if you
build and deploy your website on a remote server or a &lt;a href="https://en.wikipedia.org/wiki/CI/CD">CI/CD&lt;/a> system.&lt;/p>
&lt;/div>
&lt;p>If you ran the &lt;a href="#module-imports-example">Quick Example&lt;/a>, you will see this (as of
&lt;span class="timestamp-wrapper">&lt;span class="timestamp">&amp;lt;2022-05-26 Thu&amp;gt;&lt;/span>&lt;/span>) in your &lt;code>go.mod&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">module foo
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">go 1.18
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">require gitlab.com/kaushalmodi/hugo-mwe-theme v0.1.1 // indirect
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>.. and this in your &lt;code>go.sum&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">gitlab.com/kaushalmodi/hugo-mwe-theme v0.1.1 h1:FyTp43CJRpBfoHyWnwQFx//cipgP6xQ9/uucj+qjj1U=
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">gitlab.com/kaushalmodi/hugo-mwe-theme v0.1.1/go.mod h1:vvq0r/SfKMbiPbyqL4YottSOkpCkBSosqGRm82aDNrU=
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="updating-the-theme">&lt;span class="section-num">4&lt;/span> Updating the theme&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#updating-the-theme">#&lt;/a>&lt;/h2>
&lt;p>Here are some common ways to update the theme module going forward:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>hugo mod get -u&lt;/code>&lt;/td>
&lt;td>Update only the modules that your site directly depends on.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>hugo mod get -u ./...&lt;/code>&lt;/td>
&lt;td>Update the modules that your site depends on in a recursive fashion.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Additionally, you might or might not need these, but I am documenting
them here for completeness:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>Description&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>hugo mod get -u &amp;lt;module path&amp;gt;&lt;/code>&lt;/td>
&lt;td>Update only the specified module&lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup> to the latest version. Example: &lt;code>hugo mod get -u gitlab.com/kaushalmodi/hugo-mwe-theme&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>hugo mod get &amp;lt;module path&amp;gt;@&amp;lt;git ref&amp;gt;&lt;/code>&lt;/td>
&lt;td>Update a module to the specified git tag or commit. Example: &lt;code>hugo mod get gitlab.com/kaushalmodi/hugo-mwe-theme@v0.1.1&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="dependency-graph">Dependency Graph&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#dependency-graph">#&lt;/a>&lt;/h2>
&lt;p>If you have a theme added as a Hugo Module, which depends on other
Hugo Modules, it&amp;rsquo;s often helpful to know the dependency graph. You can
do that by running:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">hugo mod graph
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For the above &lt;a href="#module-imports-example">Quick Example&lt;/a>, you will see just this one line because
that theme does not depend on other modules:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">foo gitlab.com/kaushalmodi/hugo-mwe-theme@v0.1.1
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="building-your-hugo-site-on-a-server">Building your Hugo site on a server&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#building-your-hugo-site-on-a-server">#&lt;/a>&lt;/h2>
&lt;p>Alright, so you are able to build your site locally after switching to
using themes as modules, great!&lt;/p>
&lt;p>Now, if you build and deploy your site on a remote server like Netlify
or Vercel, you need to ensure that you have a recent version of Go
installed in their environment too.&lt;/p>
&lt;p>I deploy this website using Netlify, and so I know how to do that
there &amp;mdash; Set the &lt;code>GO_VERSION&lt;/code> environment variable to a recent
version like &lt;strong>1.18&lt;/strong> in the &lt;a href="https://docs.netlify.com/configure-builds/environment-variables/">Environment variables&lt;/a> section in Netlify
&lt;em>Build &amp;amp; deploy&lt;/em> settings.&lt;/p>
&lt;h2 id="in-a-nutshell">In a nutshell&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#in-a-nutshell">#&lt;/a>&lt;/h2>
&lt;ol>
&lt;li>&lt;strong>First&lt;/strong> convert your Hugo site to a Hugo module.&lt;/li>
&lt;li>Then replace the &lt;code>theme&lt;/code> in your site config with a module import.&lt;/li>
&lt;/ol>
&lt;p>Enjoy! 🍾&lt;/p>
&lt;h2 id="references">References&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#references">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://gohugo.io/hugo-modules/use-modules/">Hugo Modules documentation&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.thenewdynamic.com/article/hugo-modules-everything-from-imports-to-create/">Hugo Modules: Everything you need to know!&lt;/a>&lt;/li>
&lt;/ul>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>It is recommended to &lt;em>git commit&lt;/em> the &lt;code>go.sum&lt;/code> along with your
site&amp;rsquo;s &lt;code>go.mod&lt;/code>. From the &lt;a href="https://github.com/golang/go/wiki/Modules#releasing-modules-all-versions">Go Modules documention&lt;/a>: &lt;em>Ensure your
&lt;code>go.sum&lt;/code> file is committed along with your &lt;code>go.mod&lt;/code> file. See &lt;a href="https://github.com/golang/go/wiki/Modules#should-i-commit-my-gosum-file-as-well-as-my-gomod-file">FAQ
below&lt;/a> for more details and rationale.&lt;/em>&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>Trust me.. once you get a hang of the Hugo Module system, your
site will have more than one!&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/hugo">hugo</category><category domain="https://scripter.co/series/hugo-modules">Hugo Modules</category><category domain="https://scripter.co/tags/module">module</category><category domain="https://scripter.co/tags/100daystooffload">100DaysToOffload</category><category domain="https://scripter.co/tags/theme">theme</category><category domain="https://scripter.co/tags/component">component</category><guid>https://scripter.co/hugo-modules-importing-a-theme/</guid><pubDate>Thu, 26 May 2022 16:26:00 -0400</pubDate></item><item><title>Hugo Modules: Getting Started</title><link>https://scripter.co/hugo-modules-getting-started/</link><description>&lt;blockquote>What are Hugo Modules and should you convert your Hugo site to be one?&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#why-use-hugo-modules">Why use Hugo Modules?&lt;/a>&lt;/li>
&lt;li>&lt;a href="#how-to-use-hugo-modules">How to use Hugo Modules?&lt;/a>
&lt;ul>
&lt;li>&lt;a href="#install-a-recent-version-of-go">&lt;span class="section-num">1&lt;/span> Install a recent version of &lt;code>go&lt;/code>&lt;/a>&lt;/li>
&lt;li>&lt;a href="#convert-to-hugo-module">&lt;span class="section-num">2&lt;/span> Convert your Hugo site to a &lt;em>Hugo Module&lt;/em>&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;a href="#example-hugo-mod-init">Example: &lt;code>hugo mod init&lt;/code>&lt;/a>&lt;/li>
&lt;li>&lt;a href="#closing">Closing&lt;/a>&lt;/li>
&lt;li>&lt;a href="#references">References&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;div class="org-center">
&lt;p>The short answer to the second question is &lt;em>yes&lt;/em>!&lt;/p>
&lt;/div>
&lt;p>The &lt;strong>Hugo Modules&lt;/strong> feature
&lt;span class="sidenote-number">&lt;small class="sidenote">
This feature was added to Hugo back in &lt;a href="https://github.com/gohugoio/hugo/releases/tag/v0.56.0">July 2019&lt;/a> and it has become
more stable and feature-rich with time.
&lt;/small>&lt;/span>
allows collecting different pieces of your Hugo site source from
different repositories. Here each &amp;ldquo;piece&amp;rdquo; is a &lt;em>module&lt;/em>. Those modules
do not have to be just themes &amp;mdash; they can even be parts of your site
content.&lt;/p>
&lt;p>From the &lt;a href="https://gohugo.io/hugo-modules/">documentation&lt;/a>:&lt;/p>
&lt;blockquote>
&lt;p>A module can be your main project or a smaller module providing one or
more of the 7 component types defined in Hugo: &lt;strong>static&lt;/strong>, &lt;strong>content&lt;/strong>,
&lt;strong>layouts&lt;/strong>, &lt;strong>data&lt;/strong>, &lt;strong>assets&lt;/strong>, &lt;strong>i18n&lt;/strong>, and &lt;strong>archetypes&lt;/strong>. You can combine
modules in any combination you like, and even mount directories from
non-Hugo projects, forming a big, virtual union file system.&lt;/p>
&lt;/blockquote>
&lt;p>I find the Hugo Modules feature to be
&lt;mark>kind of like &lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Submodules">Git Submodules&lt;/a>&lt;/mark> , or like a symbolic link aggregation scheme like &lt;a href="https://www.gnu.org/software/stow/">GNU Stow&lt;/a> but version
controlled.&lt;/p>
&lt;div class="note">
&lt;p>You need to install &lt;code>go&lt;/code> in order to use Hugo Modules.&lt;/p>
&lt;/div>
&lt;p>So you might wonder why you would want to go through that trouble ..&lt;/p>
&lt;h2 id="why-use-hugo-modules">Why use Hugo Modules?&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#why-use-hugo-modules">#&lt;/a>&lt;/h2>
&lt;dl>
&lt;dt>Separation of reusable components&lt;/dt>
&lt;dd>This benefit is same as that of
using &lt;em>git submodules&lt;/em> for managing themes. You might have separate
repos for &lt;a href="https://github.com/kaushalmodi/hugo-atom-feed">adding ATOM feed&lt;/a>, another for adding &lt;a href="https://github.com/kaushalmodi/hugo-search-fuse-js">search&lt;/a> which you
would want to reuse on multiple sites, and you integrate those in
your main site repo as submodules. Hugo modules allows you to do the
same, and then more (recursive module updates, printing dependency
graphs, etc.).&lt;/dd>
&lt;dt>Mounts&lt;/dt>
&lt;dd>Once a site repo is a Hugo module, it can start using the
&lt;a href="https://gohugo.io/hugo-modules/configuration/#module-config-mounts">&lt;strong>mounts&lt;/strong> feature&lt;/a>.
&lt;p>Let&amp;rsquo;s call the main Hugo site repository the &lt;span class="org-radio" id="org-radio--self-module">self module&lt;/span>.
&lt;em>Mounts&lt;/em> are analogous to creating symbolic links
&lt;span class="sidenote-number">&lt;small class="sidenote">
If you know what GNU Stow does, it&amp;rsquo;s kind of like that.
&lt;/small>&lt;/span>
from any file or directory in the &lt;a href="#org-radio--self-module">self module&lt;/a> to &lt;strong>any&lt;/strong> file or
directory in one or more of the imported &lt;em>modules&lt;/em> (including the
&lt;a href="#org-radio--self-module">self module&lt;/a>).&lt;/p>
&lt;p>As an example, you might have a &lt;code>README.md&lt;/code> in the root of your git
repository. Using &lt;em>mounts&lt;/em>, you can tell Hugo to use that same file
as if it were &lt;code>content/_index.md&lt;/code>
&lt;span class="sidenote-number">&lt;small class="sidenote">
You can find the full example in &lt;a href="https://github.com/bep/portable-hugo-links/blob/aa096af0d46e5fc8126b0afaedac557470128cf1/config.toml#L12-L14">github.com/bep/portable-hugo-links&lt;/a>.
&lt;/small>&lt;/span>
, and now the same &lt;code>README.md&lt;/code> serves as the content page for your
site&amp;rsquo;s home page! 🤯&lt;/p>
&lt;/dd>
&lt;/dl>
&lt;h2 id="how-to-use-hugo-modules">How to use Hugo Modules?&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#how-to-use-hugo-modules">#&lt;/a>&lt;/h2>
&lt;h3 id="install-a-recent-version-of-go">&lt;span class="section-num">1&lt;/span> Install a recent version of &lt;code>go&lt;/code>&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#install-a-recent-version-of-go">#&lt;/a>&lt;/h3>
&lt;p>The &lt;em>Hugo Modules&lt;/em> feature is powered by &lt;a href="https://github.com/golang/go/wiki/Modules">Go Modules&lt;/a>. So even if you
don&amp;rsquo;t develop anything in &lt;code>go&lt;/code>, you need to have a recent version of
&lt;code>go&lt;/code> installed (version 1.12
&lt;span class="sidenote-number">&lt;small class="sidenote">
If you are using Netlify to deploy your website, go to the &lt;em>Deploy
Settings&lt;/em> ➔ &lt;em>Environment&lt;/em>, and set the &lt;code>GO_VERSION&lt;/code> environment
variable to 1.12 or newer.
&lt;/small>&lt;/span>
or newer).&lt;/p>
&lt;div class="note">
&lt;p>As of &lt;span class="timestamp-wrapper">&lt;span class="timestamp">&amp;lt;2022-02-24 Thu&amp;gt;&lt;/span>&lt;/span>, the latest stable version of &lt;code>go&lt;/code> is
&lt;em>1.17.7&lt;/em>. Download it from &lt;a href="https://go.dev/dl/">https://go.dev/dl/&lt;/a>
&lt;span class="sidenote-number">&lt;small class="sidenote">
Also see &lt;a href="https://scripter.co/installing-go-toolchain/">Installing go toolchain&lt;/a>.
&lt;/small>&lt;/span>
.&lt;/p>
&lt;/div>
&lt;h3 id="convert-to-hugo-module">&lt;span class="section-num">2&lt;/span> Convert your Hugo site to a &lt;em>Hugo Module&lt;/em>&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#convert-to-hugo-module">#&lt;/a>&lt;/h3>
&lt;p>For your Hugo site to be able to use themes or theme components (like
ATOM feed support, etc.), or even to use the &lt;em>Mounts&lt;/em> feature,
&lt;mark>your Hugo site itself needs to become a Hugo Module&lt;/mark> .&lt;/p>
&lt;p>Here&amp;rsquo;s how you initialize your site repo to be a Hugo Module. The
following command assumes that you Hugo site repo lives at
&lt;code>https://github.com/USER/PROJECT&lt;/code>.&lt;/p>
&lt;p>&lt;a id="code-snippet--hugo-mod-init-example">&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># cd /path/to/your/site/repo/&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">hugo mod init github.com/USER/PROJECT
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="src-block-caption">
&lt;span class="src-block-number">&lt;a href="#code-snippet--hugo-mod-init-example">Code Snippet 1&lt;/a>:&lt;/span>
Initializing your site repo to become a Hugo Module
&lt;/div>
&lt;p>Here, &lt;code>github.com/USER/PROJECT&lt;/code> is your Go/Hugo &lt;span class="org-radio" id="org-radio--module-name">module name&lt;/span>.
This name does not need to be your git remote&amp;rsquo;s URL. It could be any
globally unique string you can think of.&lt;/p>
&lt;div class="note">
&lt;p>Note that &amp;ldquo;https​://&amp;rdquo; should not be included in the &lt;em>&lt;a href="#org-radio--module-name">module name&lt;/a>&lt;/em>.&lt;/p>
&lt;/div>
&lt;p>If all went well, a &lt;code>go.mod&lt;/code> file will be created that will look like
this:&lt;/p>
&lt;p>&lt;a id="code-snippet--example-go-mod">&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">module github.com/USER/PROJECT
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">go 1.17
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="src-block-caption">
&lt;span class="src-block-number">&lt;a href="#code-snippet--example-go-mod">Code Snippet 2&lt;/a>:&lt;/span>
Example &lt;code>go.mod&lt;/code>
&lt;/div>
&lt;p>Right now, the &lt;code>go.mod&lt;/code> only contains your site&amp;rsquo;s &lt;a href="#org-radio--module-name">module name&lt;/a> and the
&lt;code>go&lt;/code> version used to create it. Once other modules are added as
dependencies, they will be added in this same file
&lt;span class="sidenote-number">&lt;small class="sidenote">
Coming back to the &lt;em>git submodules&lt;/em> analogy, &lt;code>go.mod&lt;/code> is similar to
the &lt;code>.gitmodules&lt;/code> file.
&lt;/small>&lt;/span>
.&lt;/p>
&lt;p>Also, once other module dependencies are added, a &lt;code>go.sum&lt;/code> file would
also be created containing the checksums of all your module
dependencies.&lt;/p>
&lt;p>If you are managing your Hugo themes via &lt;em>git submodules&lt;/em>, don&amp;rsquo;t
worry! Converting your site to a Hugo Module will not break anything.&lt;/p>
&lt;h2 id="example-hugo-mod-init">Example: &lt;code>hugo mod init&lt;/code>&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#example-hugo-mod-init">#&lt;/a>&lt;/h2>
&lt;p>I have a &lt;abbr aria-label="Minimum Working Example" tabindex=0>MWE&lt;/abbr> repository called
&lt;a href="https://gitlab.com/hugo-mwe/hugo-mwe">&lt;code>hugo-mwe&lt;/code>&lt;/a> to conduct small Hugo-related experiments, or play with a
new feature, or attempt to reproduce a bug that I&amp;rsquo;d want to
report. You can clone it and quickly try out the &lt;code>hugo mod init&lt;/code>
command.&lt;/p>
&lt;p>&lt;a id="code-snippet--hugo-mod-init-hugo-mwe">&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">git clone https://gitlab.com/hugo-mwe/hugo-mwe
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">cd&lt;/span> hugo-mwe
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">hugo mod init gitlab.com/&lt;span class="nv">$USER&lt;/span>/hugo-mwe
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="src-block-caption">
&lt;span class="src-block-number">&lt;a href="#code-snippet--hugo-mod-init-hugo-mwe">Code Snippet 3&lt;/a>:&lt;/span>
Full example of initializing a &lt;i>Hugo Module&lt;/i>
&lt;/div>
&lt;h2 id="closing">Closing&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#closing">#&lt;/a>&lt;/h2>
&lt;p>In this post, I have given a brief introduction to what &lt;em>Hugo Modules&lt;/em>
are, and how you can convert your Hugo site into one. In the &lt;a href="https://scripter.co/hugo-modules-importing-a-theme/">next
post&lt;/a>, I go through how to import themes as &lt;em>modules&lt;/em>.&lt;/p>
&lt;h2 id="references">References&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#references">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://gohugo.io/hugo-modules/">Hugo Modules documentation&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://discourse.gohugo.io/t/hugo-modules-for-dummies/20758">Hugo Forum &amp;ndash; Hugo Modules for &amp;ldquo;dummies&amp;rdquo;&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.thenewdynamic.com/article/hugo-modules-everything-from-imports-to-create/">Hugo Modules: Everything you need to know!&lt;/a>&lt;/li>
&lt;/ul></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/hugo">hugo</category><category domain="https://scripter.co/series/hugo-modules">Hugo Modules</category><category domain="https://scripter.co/tags/module">module</category><category domain="https://scripter.co/tags/100daystooffload">100DaysToOffload</category><guid>https://scripter.co/hugo-modules-getting-started/</guid><pubDate>Thu, 24 Feb 2022 02:21:00 -0500</pubDate></item></channel></rss>