Using WSL and Node.js to build and manage Hexo

I had initially intended to publish this as part of my Azure DevOps entry, but it felt like it needed its own dedicated article. For my Hexo workflow I decided I wanted to use the Windows Subsystem for Linux from here on referred to as WSL. This works best for me because I am primarily a Windows user and I also have a fair bit of experience with Linux.

Getting Started

The first thing you’ll need to do is get Hexo configured locally so that you can generate the static content for later deployment, and also run your local server to test your content. The basics on how to configure Hexo are available on their website, I’ll cover how to do it on WSL specifically.

The first thing you need to do if you have not already is get WSL installed and running. Step by step instructions on this are available over on Microsoft Docs. The instructions below are specifically for the Ubuntu distro.

The below instructions assume a fresh install of the Ubuntu distro from the Windows store.

Getting npm and Node.js installed

You’ll need git for a variety of reasons, so let’s just get it installed first along with all of the prerequisites.

Install Git

sudo apt-get install git-core

Install NVM & Node.js

This is using Node Version Manager by creationix on GitHub. There are many ways to install Node.js, I went with the Hexo documentation.

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh

Run installation script:

sh install.sh

Load NVM if it isn’t already loaded:

1
2
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

FInally, install node.js

nvm install stable

Install npm

In this step we’ll install the node package manager. npm is a package manager for node that will then allow you to install Hexo, plugins, and anything else you’d like to do with node.

sudo apt install npm

Install Hexo

At this point we are ready to install the Hexo cli which will allow you to finally build out a hexo environment.

npm install -g hexo-cli

Initialize a new blog

With WSL you can actually navigate to the Windows file system which means you can utilize tools like Visual Studio Code to edit your articles, templates and configuration files. For purposes of this article lets say we want to get Hexo running in c:\hexo

Note:
You can actually customize the mount point noted below. For example you could change it to /c/hexo instead of /mnt/c/hexo by modifying wsl.conf. You can also use these steps if you find that your Windows drives are not mounted inside WSL.

1
2
3
[automount]
root = /
options = "metadata"

Full documentation on settings available in wsl.conf is available here.

Now initialize a new site in that location:

1
2
3
hexo init /mnt/c/hexo
cd /mnt/c/hexo
npm install

You will then have the following folder structure:

.
├── _config.yml
├── package.json
├── scaffolds
├── source
|   ├── _drafts
|   └── _posts
└── themes

That’s it! Now you are ready to configure your blog and start authoring content. Documentation for editing your site configuration can be found here.

Finishing Up

Now that you have the basics in place you can commit the directory to GitHub or Azure Devops and begin authoring posts and even installing custom themes if you want. Below are a few links to get you started:

  • Writing Content - This explains how to write new posts and pages as well as create drafts.
  • Miscellaneous Commands - This contains documentation on how to generate a static site, or launch a local server to test your newly created content.

I hope this article was helpful! You are now well on your way to publishing a Hexo blog in the wild. In my next article I will cover how to automatically deploy Hexo using an Azure Pipeline.

Why I chose Hexo for my Technical Blog

When the time came to relaunch my blog I spent a fair amount of time researching options. I wanted something that worked well for me that didn’t require too much infrastructure. I also wanted to keep costs fairly low and not take on a large monthly recurring cost.

  • Ideally, I wanted static content that I could host in an Azure Storage account or some sort of static website hosting.
  • I wanted something that felt comfortable for me. I spend a fair amount of time authoring articles in Markdown and it has really grown on me over the past few years.
  • An application with a fair amount of plugin and theme options, I don’t really want to get involved in creating a custom template. A reasonably sized community was a plus.

The Competitors

I evaluated a variety of different options before settling on Hexo. Obviously I’m sure there are more options out there, but this is what I looked at.

  • DocFX - I’ve had some experience with DocFX in the past and honestly, it is what I really wanted to use. But the lack of theme selection and the fact it’s primarily focused on documentation meant it was not a great fit for what I wanted to do. It is the engine behind Microsoft Docs and it does that really well so I will continue to monitor the project.
  • Hugo - A close second, Hugo is very similar to Hexo. It is built on Go rather than Node.js. It generates a static site that you can publish in much the same way. The configuration seemed a little bit more involved, but in the end it was almost a coin flip. Now that my content is in Markdown and ready to roll I may still give Hugo a try to compare and contrast.
  • Ghost - Their hosted platform pricing was a bit steep for me. And while they did offer self hosting the config seemed fairly complex and the requirements were also steep. I also don’t really need the fancy admin interface.
  • Medium - This is where my blog was before. It is a great platform for getting your content out there. But it’s not really a great platform for the true technical blogging that I wanted to do. I do still intend to post opinion pieces.. but not enough that Mediums core benefit would be that useful.
  • Wordpress - Obviously this option is one of the largest out there. And there are definitely a ton of options for plugins and themes. And I have experience with it in the past. But for me it felt like using a bulldozer to solve something I could use a shovel for.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×