To relaunch the blog, I chose Hexo as the framework to use to statically generate the new site. In this article, I’ll detail the steps to get everything set up and ready to publish new posts.
Install Node.js
Hexo is written in Javascript and uses Node.js to run. That means the first order of business is to get Node.js installed.
If you do a fair amount of Node.js development, you will quickly find that you often need different versions of Node.js installed for different projects. There are a number of projects available to manage multiple installs (nvm, nodist for Windows, n, etc.), but I tend to favor nodenv.
Nodenv works by installing the different versions of Node in your home directory under the .nodenv
directory. Switching between them involves running a command that sets an environment variable (NODENV_VERSION
) or creates a .node-version
file in your project directory (preferred).
I’m using WSL2 on a Windows machine using brew, so the install is pretty straightforward:
1 | # Install nodenv |
This sets up your environment so that the version of node automatically switches, depending on which directory you’re currently in. If there’s a .node-version
file, it uses the version specified there. Otherwise, it uses the version we set with the nodenv global
command.
Now that I have Node installed, it’s time to install Hexo and get it set up.
Install Hexo
Getting Hexo installed is a bit of a chicken-and-egg problem. To set up a new Hexo project I need Hexo installed but to install Hexo, I need a Node project.
I like to keep my Node.js installs as clean as possible, meaning that I don’t generally install NPM packages globally. Rather, I prefer to let each project manage it’s own dependencies. That way the dependencies of one project don’t interfere with another and I’m assured that the final build I make will work when I go to deploy to a production environment.
To bootstrap Hexo, we’ll install it globally, set up the Hexo project and then remove the global installation. We can then rely on the project specific version of Hexo for future development.
1 | # Install Hexo |
At this point, we have a working blog! The project initialization creates the project skeleton and populates it with a sample blog post. This sample blog post, source/_posts/hello-world.md
, provides helpful documentation about how to get started with Hexo–how to start the server, generate the static site, manage posts, and more.
You can view the output of your work thus far by starting Hexo in development (server) mode:
1 | $ hexo server |
Following the instructions above, you can now view your locally running blog by visiting the url http://localhost:4000.
Source Code Control
As mentioned in part 1, my backup strategy will be based on source control. Now is a great time to set this up. It’s good to get in the habit of making regular commits each time something new is added and working with the blog. If I really mess something up, I can always roll back to the last good commit and try again.
GitHub is pretty much the standard these days. It’s what I’ll be using for this project, but you could just as easily use GitLab or your own, self-hosted repository. I prefer to use one of the cloud-based providers, as it gets a copy of my website off my laptop and out of my home, should something happen to either one.
Following the instructions from GitHub, the basic sequence is as follows:
- Initialize your local directory as a git repository
1
git init -b main
- Commit the initial state of the directory as your first commit
1
2git add .
git commit -m "Hexo setup complete" - Set up a repository on GitHub.com. I chose to make mine private, but feel free to choose the permissions you’re comfortable with.
- Connect your GitHub.com repository to your local directory:
1
2
3
4
5
6# REMOTE_URL is copied from the GitHub.com page for your repository
git remote add origin <REMOTE_URL>
# Verify the remote is working
git remote -v
# Push your local commits to GitHub.com
git push origin main
Ready for Import
Now that Hexo is setup, I’m ready to import the 50+ blog posts from my old website. Stay tuned for part 3 of this series for how I accomplished that!
Getting a new blog up and running may seem like a daunting task. By breaking down the process into small pieces, and tackling each piece on a consistent basis, the new blog will be up and running in no time!