Automagically install Hugo (https://gohugo.io) as part of
npm install
without using got.
Hugo is one of the most popular and best static site generators. If you read this, you chose npm as your package manager. However, Hugo is written in Go and not installable with npm.
Don't worry, Hugo Install, is a JS script which downloads the correct Hugo binary, e.g. via the postinstall
script automagically as part of npm install
. Nice!
The JS script installs the extended Hugo version if possible (because it was much easier to code). For usage within corporate networks or behind corporate proxies, the download url can of course be overwritten.
Run the following command to add hugo-install to your devDependencies
in package.json.
npm install hugo-install --save-dev
All other Javascript package manager also work (this package is developed with Bun).
This project uses native fetch, so you need nodejs version >= 18.0.0.
I recommend to run the script as part of your postinstall
script. The Hugo version must be set using the --version
/-v
CLI argument. For example:
{
"scripts": {
"postinstall": "hugo-install -v 0.123.2"
}
}
Important: Make sure to use the exact version number as used in the official Hugo GitHub releases (e.g. trailing zeros that exist or do not exist).
hugo-install supports overwriting the download url. There are multiple ways to do this:
Pass the --url
/-u
CLI argument.
{
"scripts": {
"postinstall": "hugo-install -v 0.123.2 -u https://example.com/hugo/v0.123.2/hugo_0.123.2_freebsd-amd64.tar.gz"
}
}
As local or global .npmrc configuration file
hugo_install_url = "https://example.com/hugo/v0.123.2/hugo_0.123.2_freebsd-amd64.tar.gz"
export HUGO_INSTALL_URL="https://example.com/hugo/v0.123.2/hugo_0.123.2_freebsd-amd64.tar.gz"
Note that you have to run the postinstall
script again with npm install
.
When the binary is fetched, it is usable as command, as part of an npm script or from within another JS script.
Set alias hugo="npm run hugo
in your shell. Then nothing changes in your workflow.
The first script is required. The others are to make your life easier.
{
"scripts": {
"hugo": "hugo",
"build": "hugo",
"new": "hugo new",
"serve": "hugo server"
}
}
To create a new post run:
npm run create content post/my-new-post.md
To execute the Hugo Binary using the Node.JS spawn
function do for example this:
import { spawn } from "node:child_process";
import hugoPath from "hugo-install";
spawn(hugoPath, ["--config=path/to/config.toml"], {
stdio: "inherit",
}).on("exit", () => {
// Callback
});
Thank you!
MIT © Frederik Zorn
Made with ❤️