Hexo Introduction
What is Hexo
Hexo is a fast, simple and powerful static blog framework powered by Node.js. You only need to write in Markdown, and Hexo generates pure static HTML pages in seconds — no database, no PHP or other backend environment required. Any static hosting (GitHub Pages, Vercel, Nginx, object storage, etc.) can serve it directly.
Compared with dynamic blog systems, Hexo’s advantages:
- Fast: static pages, no server-side rendering, CDN-friendly
- Simple: write in Markdown, version with Git, deploy with one command
- Powerful: a huge ecosystem of plugins and themes (this tutorial is based on the deeply customized matery theme)
- Secure: no database, no runtime attack surface, naturally resistant to attacks
This blog (夜法之书) is built on Hexo 8.1.2 + matery theme, hosted in production by Vercel.
Environment Requirements
| Component | Version | Notes |
|---|---|---|
| Node.js | >= 20 | Hexo 8.x runtime (this site uses the LTS version; package.json declares no engines field — Node >= 20 is the actual runtime requirement) |
| npm | bundled with Node (project locks npm@9) | Installed with Node.js, used to install Hexo and plugins |
| Git | any modern version | Clone themes, deploy to Git hosting platforms |
Verifying the Environment
Output: two version lines, the second followed by npm’s own update notice (only when a newer version exists). As long as Node’s major version is >= 20 and npm’s is 9, the environment is fine; a command not found means the tool is not installed or not on PATH.
v20.19.4
9.9.4Corresponding command:
node -v # something like v20.x
npm -v # 9.x (project packageManager locks to npm@9)Common Commands
| Command | Purpose |
|---|---|
hexo init [folder] | Create a new site. Equivalent to cloning hexo-starter and installing dependencies |
hexo new post <title> | Create a post in source/_posts/ (quote the title if it contains spaces) |
hexo new page <name> | Create a standalone page (e.g. about, tags) |
hexo new draft <title> | Create a draft in source/_drafts/ |
hexo publish <title> | Publish a draft as a regular post |
hexo generate | Generate static files to public/ (-w watch for changes, -d deploy after generating) |
hexo server | Start a local preview server, default http://localhost:4000/ (-p to set the port) |
hexo deploy | Deploy the site (push to the configured Git repo, -g generates before deploying) |
hexo clean | Clear the cache (db.json) and the generated directory (public); recommended after changing config/plugins |
hexo --safe | Safe mode: skip plugins and scripts; use when debugging plugin conflicts |
hexo server --draft | Show drafts in preview |
hexo version | Show the Hexo version |
What Each Command Shows You
The table above is expanded below into Output → Corresponding command: first the feedback from the terminal/filesystem, then the command to copy.
① Create a site
Output: the terminal prints INFO Cloning hexo-starter ..., INFO Install dependencies, and finally INFO Start blogging with Hexo!; a my-blog/ directory appears with _config.yml, package.json, source/, themes/ inside.
Corresponding command:
hexo init my-blog
cd my-blog
npm install② Create a post
Output: the terminal prints INFO Created: ~/my-blog/source/_posts/Post-title.md; opening that file shows title / date / tags and other front-matter already pre-filled from scaffolds/post.md.
Corresponding command:
hexo new post "Post title"③ Create a standalone page
Output: the terminal prints INFO Created: ~/my-blog/source/about/index.md, and the file contains only title and date (layout must be added by hand).
Corresponding command:
hexo new page about④ Create a draft
Output: the terminal prints INFO Created: ~/my-blog/source/_drafts/Unfinished-idea.md; it is not generated by default, so the homepage does not show it.
Corresponding command:
hexo new draft "Unfinished idea"⑤ Publish a draft
Output: the terminal prints INFO Published: ...source/_posts/Unfinished-idea.md; the file moves from _drafts/ to _posts/ and date is written with the current time.
Corresponding command:
hexo publish "Unfinished idea"⑥ Generate the static site
Output: the terminal lists INFO Generated: index.html, INFO Generated: posts/xxx/index.html and so on, ending with INFO Files generated in 3.2s (timing varies by machine); public/ now holds the complete HTML site.
Corresponding command:
hexo generate⑦ Local preview
Output: the terminal prints Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.; opening that address in a browser shows the homepage. The browser refreshes automatically after you edit a file.
Corresponding command:
hexo server⑧ Deploy
Output: the terminal prints INFO Deploying: git, INFO Clearing .deploy_git folder..., and finally INFO Deploy done: git; the hosting platform starts building.
Corresponding command:
hexo deploy⑨ Clean the cache
Output: the terminal prints INFO Deleted database. and INFO Deleted public folder.; db.json and public/ are gone, which resolves most “my change doesn’t take effect” problems.
Corresponding command:
hexo clean⑩ Safe mode
Output: the site still starts, but theme scripts and custom tag plugins are all skipped (pages look plainly “bare”), which tells you whether the fault comes from a plugin.
Corresponding command:
hexo --safe server⑪ Preview drafts
Output: the same http://localhost:4000/ as a normal preview, except the homepage list now includes posts from _drafts/.
Corresponding command:
hexo server --draft⑫ Show the version
Output: one line hexo: 8.1.2 plus environment info such as os, node, npm — paste the whole block when reporting an issue.
Corresponding command:
hexo versionCommon Combinations (this project’s package.json scripts)
Output: npm run build leaves public/ ready and also builds the pagefind search index; npm run clean empties the artifacts; npm run server serves on http://localhost:4000/; npm run deploy pushes to the deploy repo.
Corresponding command:
npm run build # hexo generate (also builds the pagefind search index)
npm run clean # hexo clean
npm run server # hexo server
npm run deploy # hexo deployWorkflow
Write (Markdown) → Generate (hexo generate) → Deploy (hexo deploy)Write:
hexo new post "Post title"creates a post; edit the file undersource/_posts/in MarkdownOutput:
source/_posts/Post-title.mdappears with front-matter already filled in. Corresponding command:hexo new post "Post title"Preview:
hexo serverchecks the result locally (defaultlocalhost:4000)Output: the browser shows pages structured like production; saving a file hot-reloads it. Corresponding command:
hexo serverGenerate:
hexo generaterenders posts into static files underpublic/Output: artifacts such as
public/index.htmlandpublic/posts/<abbrlink>/index.html. Corresponding command:hexo generateDeploy:
hexo deploypushes to a Git repo; or push to GitHub and let platforms like Vercel build automaticallyOutput: the terminal prints
INFO Deploy done: git, and the hosting platform starts building. Corresponding command:hexo deploy
Tip: after modifying
_config.ymlor theme config, always runhexo cleanbeforehexo generate, otherwise the cache may cause changes not to take effect.
Next Steps
- Installation & Theme Setup: build a site with the matery theme
- Directory Structure & Content Management: understand file layout and front-matter