Hexo 使用过程遇到的一些问题收集

Author Avatar
dev.liang 6月 03, 2017
  • 在其它设备中阅读本文章

最近因为公司统一为我们配置了新 Mac 电脑,想在新电脑上使用自己的 Hexo,就从旧电脑上把 Hexo 文件夹全部 copy 过来了。

然后在新电脑上执行了

npm install -g hexo-cli

安装下 hexo 环境,完成以后想着部署下 Hexo,就执行了

hexo clean

clean 后报了一大串异常:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Error: Module version mismatch. Expected 48, got 57.
at Error (native)
at Object.Module._extensions..node (module.js:597:18)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/bunyan/lib/bunyan.js:79:18)
{ Error: Cannot find module './build/default/DTraceProviderBindings'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/bunyan/lib/bunyan.js:79:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32) code: 'MODULE_NOT_FOUND' }
{ Error: Cannot find module './build/Debug/DTraceProviderBindings'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/bunyan/lib/bunyan.js:79:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32) code: 'MODULE_NOT_FOUND' }
INFO Deleted database.
INFO Deleted public folder.

执行

hexo generate #使用 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Error: Module version mismatch. Expected 48, got 57.
at Error (native)
at Object.Module._extensions..node (module.js:597:18)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/bunyan/lib/bunyan.js:79:18)
{ Error: Cannot find module './build/default/DTraceProviderBindings'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/bunyan/lib/bunyan.js:79:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32) code: 'MODULE_NOT_FOUND' }
{ Error: Cannot find module './build/Debug/DTraceProviderBindings'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/local/lib/node_modules/hexo-cli/node_modules/bunyan/lib/bunyan.js:79:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32) code: 'MODULE_NOT_FOUND' }
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
[master 1cf33bb] Site updated: 2017-06-03 02:24:53
13 files changed, 103 insertions(+), 103 deletions(-)
rename 2017/03/{13 => 27}/hello-world/index.html (93%)
rewrite content.json (92%)
Username for 'https://github.com': leaderliang
Password for 'https://leaderliang@github.com':
To https://github.com/leaderliang/leaderliang.github.io.git
a268493..1cf33bb HEAD -> master
Branch master set up to track remote branch master from https://github.com/leaderliang/leaderliang.github.io.git.
INFO Deploy done: git

网上搜了很多解决方案:

npm install –no-optional

rm -rf node_modules && npm install

等等,都不好使,后来还以为是 node 的版本和之前旧电脑上的版本不一致呢,折腾了好几个版本后,发现也不是这个原因,可执行查看 node.js 版本号

node –version

最后在 github 上找到一个方案

npm rebuild

等待执行完成,在执行 clean 操作就不报异常了

总结:因每台电脑的配置不同导致生成的 node_modules 以及一些配置文件不同造成的。

Hexo 的一些功能

草稿

在新建文件的时候,可以先写草稿,等到写好之后,再发布出去。那么,就需要草稿的功能。

1
hexo new draft "新建草稿md"

这样,文件就会创建在会在 source/_drafts 目录下,如果想在本地部署预览草稿,则可以使用

1
2
3
4
5
INFO  Created: ~/MacDev/Hexo/source/_drafts/新建草稿文件.md
# 执行
hexo s --drafts
或者
hexo s --draft

当文章写好之后,想发布出去,就要移出草稿

1
2
3
hexo publish "新建草稿文件"
or
hexo P <filename>

后续补充 hexo 在使用过程中出现的错误总结

fs.SyncWriteStream 报错 (问题借鉴相关出处

1
DeprecationWarning: fs.SyncWriteStream is deprecated

安装的node版本是8.1,而node.js从8.0开始就已经弃用了前面提到的fs.SyncWriteStream方法,然而作者项目里的一个叫hexo-fs的插件,调用了这个方法,所以就会报错,解决办法,作者没有提供,但是很显然,我只需要更新一下那个插件,就可以搞定,于是更新的方法是,在Hexo博客目录下,输入:

1
npm install hexo-fs --save

等待插件更新完毕…

image

Hexo卸载插件命令

1
2
npm uninstall 插件名 --save
例如卸载七牛云同步插件 npm uninstall hexo-qiniu-sync --save

DeprecationWarning: fs.SyncWriteStream is deprecated

node和hexo插件的版本带来的问题:在node8.x的版本中,fs.SyncWriteStream被弃用了

更新如下插件

1
2
3
4
npm install hexo-fs --save
npm install hexo-deployer-git@0.3.1 --save
npm install hexo-renderer-ejs@0.3.1 --save
npm install hexo-server@0.2.2 --save

hexo 添加 gitalk 遇到的问题

有几个参数需要说一下

1
2
3
4
gitalk_repo: leaderliang.github.io # git repo of the hexo 创建一个与应用名称一样的repo,用来存放评论
gitalk_owner: leaderliang # git repo's owner 你的github账户名
gitalk_client_id: f00**********3b7ec # github app client id
gitalk_client_secret: ff33*******bd0 # github app client secret
1
发布的新文章提示“未找到相关的 Issues 进行评论,请联系xxx初始化创建”

解决方案,OAuth Apps
Homepage URL,这里最后的/号要加,http和https没关系,都可以,因为我是https所以填了https.
https://leaderliang.github.io/
Authorization callback URL,也是最后的/号要加,同上
https://leaderliang.github.io/

hexo 版本升级命令

1
2
3
4
5
6
npm i hexo-cli -g

npm install hexo-cli -g

npm update
输入之后发现出现了一系列的 WARN

具体可参考: https://blog.csdn.net/whjkm/article/details/81088518

hexo clean && hexo g && hexo d 部署代码提示 Error: Spawn failed…

报错详情如下

1
2
3
4
5
6
7
8
9
10
11
12
Branch 'master' set up to track remote branch 'master' from 'git@github.com:leaderliang/leaderliang.github.io.git'.
On branch master
nothing to commit, working tree clean
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (/Users/liangyanqiao/MacDev/Hexo/node_modules/hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (events.js:210:5)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)

因为在 config 文件中配置的 coding 部署地址,官网调整了,在官网找到对配置文件进行修改并对 git 远端地址修改一下即可;

1
2
旧的:coding: git@git.dev.tencent.com:leaderliang/leaderliang.git 
调整为:coding: git@e.coding.net:leaderliang/Hexo/Hexo.git

Node Sass does not yet support your current environment: Windows 64-bit 异常如何解决

上面的异常是在我更新了 node 版本后出现的, 这个异常直接说了 node sass 不支持当前环境,所以可以直接删掉原来不支持本机的 node sass,再重新安装就行了。

1
2
3
4
5
删除:
npm uninstall --save node-sass

安装:
npm install --save node-sass

Hexo code hight light(目前使用的不是这个,等有时间加一下)

不错的 highlight.js 推荐,地址:https://highlightjs.org/usage/
对应的博客简单教程:https://zihengcat.github.io/2018/03/05/Hexo博客添加highlight-js代码高亮/

ERROR Plugin load failed: hexo-generator-json-content

执行命令 npm i hexo-generator-json-content -save

You must install peer dependencies yourself

类似以下问题

1
2
3
npm WARN babel-eslint@10.0.2 requires a peer of eslint@>= 4.12.1 but none is installed. You must install peer dependencies yourself.
npm WARN react-flip-move@2.9.14 requires a peer of react@0.13.x || 0.14.x || 15.x.x but none is installed. You must install peer dependencies yourself.
npm WARN react-flip-move@2.9.14 requires a peer of react-dom@0.13.x || 0.14.x || 15.x.x but none is installed. You must install peer dependencies yourself.

出现这样问题,原因是 npm 版本较低,更新 npm 版本即可

1
npm install -g npm

node 版本升级

1
npm install -g n

升级node.js到最新稳定版

1
n stable

遇到 ermission denied 就在命令行前加 sudo

was compiled against a different Node.js version using 错误问题(update 2019-11-07 19:24:49)

下面把详细报错的问题展示下,可以对比下

1
2
3
4
5
6
7
Error: The module '/usr/local/lib/node_modules/hexo-cli/node_modules/dtrace-provider/build/Release/DTraceProviderBindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 57. This version of Node.js requires
NODE_MODULE_VERSION 72. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
...
Error: Cannot find module './build/default/DTraceProviderBindings'

出现这种问题,应该是我在搞 RN 的时候把 node 版本升级到最高了
解决方法:

1
2
3
4
5
6
7
8
9
10
11
12
# 重新安装hexo
hexo npm install hexo --no--optional
# 如果执行上面命名后,还是提示错误,则先卸载再安装
hexo npm uninstall hexo-cli -g
hexo npm install hexo-cli -g

# 如果发现带上前缀不好使的话,可以把 hexo 去掉,执行 npm 的命令
npm install hexo --no--optional
npm uninstall hexo-cli -g
npm install hexo-cli -g

# 对写过的文章是没有影响的,放心执行

Hexo 添加 PDF 功能

1
2
3
4
5
6
7
8
# 安装
npm install --save hexo-pdf

# 在文章中添加
# 外部链接:
{% pdf https://leaderliang.github.io/file.pdf %}
# 本地连接:
{% pdf ./pdf/kotlin_entry_study_notes.pdf %}

npm outdated 哪些需要更新

参考: https://tommy.net.cn/2018/02/26/upgrade-hexo-to-v3-5-0/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
npm outdated
Package Current Wanted Latest Location
babel-eslint 10.0.2 10.0.3 10.0.3 hexo-site
eslint 4.12.1 4.19.1 6.6.0 hexo-site
hexo 3.9.0 3.9.0 4.0.0 hexo-site
hexo-deployer-git 1.0.0 1.0.0 2.0.0 hexo-site
hexo-fs 1.0.2 1.0.2 2.0.0 hexo-site
hexo-generator-archive 0.1.5 0.1.5 1.0.0 hexo-site
hexo-generator-category 0.1.3 0.1.3 1.0.0 hexo-site
hexo-generator-index 0.2.1 0.2.1 1.0.0 hexo-site
hexo-generator-tag 0.2.0 0.2.0 1.0.0 hexo-site
hexo-renderer-ejs 0.3.1 0.3.1 1.0.0 hexo-site
hexo-renderer-stylus 0.3.3 0.3.3 1.1.0 hexo-site
hexo-server 0.3.3 0.3.3 1.0.0 hexo-site
hexo-util 1.4.0 1.5.0 1.5.0 hexo-site
highlight.js 9.15.8 9.16.2 9.16.2 hexo-site
node-sass 4.12.0 4.13.0 4.13.0 hexo-site
react 0.14.9 0.14.9 16.11.0 hexo-site
react-dom 0.14.9 0.14.9 16.11.0 hexo-site
react-flip-move 2.9.14 2.10.2 3.0.4 hexo-site

把 Hexo 的版本号根据自己需要修改,其他的也根据情况更新一下。

都修改好了以后,就 npm 更新一下:

1
npm install --save

npm WARN ajv-keywords@2.1.1 requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.

1
npm install ajv@^5.0.0 --save 必须要指定版本才可以,要不然还是会安装失败