##起因

因为折腾多平台部署,以为是hexo版本过低,所以升级了hexo到了3.0版本。结果却发现hexo dhexo g等命令都失效了。运行会有类似提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Commands:
help Get help on Hexo.
init Create a new Hexo folder.
version Display version information.

Global Options:
--config Specify config file instead of using _config.yml
--cwd Specify the CWD
--debug Display all verbose messages in the terminal
--safe Disable all plugins and scripts
--silent Hide output on console

For more help, you can use 'hexo help [command]' for the detailed information
or you can check the docs: http://hexo.io/docs/

##尝试官方迁移方案
Hexo2.X –> Hexo3.0,官方给出了具体的升级方案

但是我在升级的时候遇到了几个问题

  1. 连接npm速度慢
  2. 安装Hexo其他插件位置不当

之后我放弃了官方的解决方案。自己尝试从头开始

##自己的解决方案

从头开始并不是把之前的博文都删掉。而是重新建立一个文件夹,运行hexo init后将原来的文章和.git文件复制过来。

  1. 切换淘宝npm镜像
    连接npm国外镜像速度太慢,解决方案是切换npm的镜像。在npm的目录下找到.npmrc添加以下代码

    1
    registry = https://registry.npm.taobao.org
  2. 安装Hexo命令行

    1
    C:\Users>npm install hexo-cli -g
  3. 新建文件夹
    这里为文件夹的名字为test

    1
    2
    3
    4
    5
    C:\Users>F:

    F:\>mkdir test

    F:\>cd test
  4. 初始化目录
    使用hexo init初始化目录即可

    1
    F:\test>hexo init

    初始化成功后会有类似提示

    INFO Copying data to F:\test
    INFO You are almost done! Don’t forget to run ‘npm install’ before you start bl
    ogging with Hexo!

  5. 安装相应的插件
    Hexo3.0最大的变化是将很多原来的功能都独立成了插件。这里的插件有文章生成插件,部署插件等。根据上面的提示使用npm install即可。

    1
    F:\test>npm install

    等待一段时间后,所有插件会自动下载完毕。如果出现一下类似提示请重新运行npm install直至不再提示

    npm ERR! registry error parsing json

    npm install完成后node_modules的文件夹结构应该是这样的。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    node_modules
    ├─hexo

    ├─hexo-generator-archive

    ├─hexo-generator-category

    ├─hexo-generator-index

    ├─hexo-generator-tag

    ├─hexo-renderer-ejs

    ├─hexo-renderer-marked

    ├─hexo-renderer-stylus

    └─hexo-server
  6. 复制原来的文章和主题当前文件夹
    如果你有使用git管理整个网站的习惯还可以将原来的.git文件夹复制过来。但是请不要直接将原来的_config.yaml复制过来。而是用到的内容才复制

  7. 生成静态文件和运行博客

    1
    F:\test>hexo g

    运行完成,生成网页成功以下类似提示

    INFO Files loaded in 3.6 s
    INFO Generated: js/script.js
    INFO Generated: fancybox/jquery.fancybox.pack.js
    INFO Generated: fancybox/jquery.fancybox.js

    INFO Generated: archives/2015/index.html
    INFO Generated: archives/2015/07/index.html
    INFO Generated: index.html
    INFO 28 files generated in 1.32 s

    使用hexo s运行博客

    1
    F:\test>hexo s

    运行成功有以下类似提示

    INFO Hexo is running at http://0.0.0.0:4000/. Press Ctrl+C to stop.

    使用浏览器打开http://localhost:4000查看博客。Hello World Again!

    如果运行hexo s出现hexo的帮助页面。也就是类似下面的提示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    Usage: hexo <command>

    Commands:
    clean Removed generated files and cache.
    config Get or set configurations.
    deploy Deploy your website.
    generate Generate static files.
    help Get help on a command.
    init Create a new Hexo folder.
    list List the information of the site
    migrate Migrate your site from other system to Hexo.
    new Create a new post.
    publish Moves a draft post from _drafts to _posts folder.
    render Render files with renderer plugins.
    version Display version information.

    Global Options:
    --config Specify config file instead of using _config.yml
    --cwd Specify the CWD
    --debug Display all verbose messages in the terminal
    --draft Display draft posts
    --safe Disable all plugins and scripts
    --silent Hide output on console

    For more help, you can use 'hexo help [command]' for the detailed information
    or you can check the docs: http://hexo.io/docs/

    请使用npm install hexo-server --save安装hexo服务器。

    类似的如果运行命令没有得到应有的效果。请安装对应的插件。以下是一些常用的插件列表安装命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    npm install hexo-server --save @安装hexo 服务器
    npm install hexo-deployer-git --save @安装hexo git部署插件
    npm install hexo-deployer-heroku --save @安装hexo heroku 部署插件
    npm install hexo-deployer-rsync --save @安装hexo同步插件
    npm install hexo-deployer-openshift --save @不知道什么鬼
    npm install hexo-renderer-marked@0.2 --save @安装hexo markdown渲染插件
    npm install hexo-renderer-stylus@0.2 --save @安装hexo 模板插件
    npm install hexo-generator-feed@1 --save @安装hexo RSS插件
    npm install hexo-generator-sitemap@1 --save @安装hexo站点地图插件