Hexo 常见问题与解决方案

记录 Hexo 升级或使用时遇到的问题和一些解决方案。

image-20201104154943836

TypeError: config._d.getTime is not a function

错误日志:

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
Unhandled rejection Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk) [Line 19, Column 14]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\_partials\head\head-unique.njk) [Line 10, Column 23]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk) [Line 3, Column 3]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\_partials\header\index.njk) [Line 6, Column 15]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\_partials\header\sub-menu.njk) [Line 2, Column 29]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\_partials\header\sub-menu.njk)
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk) [Line 5, Column 3]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\post.njk) [Line 9, Column 12]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\_macro\post.njk) [Line 214, Column 16]
Template render error: (C:\Users\user\Desktop\project\blog\themes\next\layout\_partials\post\post-related.njk)
TypeError: config._d.getTime is not a function
at Object._prettifyError (C:\Users\user\Desktop\project\blog\node_modules\nunjucks\src\lib.js:36:11)
at C:\Users\user\Desktop\project\blog\node_modules\nunjucks\src\environment.js:561:19
at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\user\Desktop\project\blog\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:45:3)
at Template.render (C:\Users\user\Desktop\project\blog\node_modules\nunjucks\src\environment.js:550:10)
at C:\Users\user\Desktop\project\blog\themes\next\scripts\renderer.js:35:29
at _View._compiled.locals [as _compiled] (C:\Users\user\Desktop\project\blog\node_modules\hexo\lib\theme\view.js:136:50)
at _View.render (C:\Users\user\Desktop\project\blog\node_modules\hexo\lib\theme\view.js:39:17)
at C:\Users\user\Desktop\project\blog\node_modules\hexo\lib\hexo\index.js:64:21
at tryCatcher (C:\Users\user\Desktop\project\blog\node_modules\bluebird\js\release\util.js:16:23)
at C:\Users\user\Desktop\project\blog\node_modules\bluebird\js\release\method.js:15:34
at RouteStream._read (C:\Users\user\Desktop\project\blog\node_modules\hexo\lib\hexo\router.js:47:5)
at RouteStream.Readable.read (_stream_readable.js:470:10)
at resume_ (_stream_readable.js:949:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

错误原因:

错误是由 hexo-related-popular-posts 引发,在该库源码中使用 moment 初始化 list.date 导致了错误。 list.date 通过打印值可以看到是一个 moment 对象,但这个 moment 对象并不规范或者说可能在某处修改了这个 moment 对象的值。

moment 内部初始化有一段逻辑是:

1
this._d = new Date(config._d != null ? config._d.getTime() : NaN);

这个 config 就是 moment(list.date) 传入的 list.date 的值。config._d 是一个时间类型的字符串,并不是 Date 类型,因此没有 getTime 的方法。

解决方案:

  • 方法1:

    • theme/next/_config.yml 中的 related_posts.params.isDate 设为 false,推荐列表中不展示时间
  • 方法2:修改源码

    • 打开文件\node_modules\hexo-related-popular-posts\lib\list-json.js

    • 修改代码

      1
      2
      3
      if (inOptions.isDate && list.date != '') {
      ret.date = moment(list.date).format(config.date_format || 'YYYY-MM-DD')
      }

      1
      2
      3
      4
      5
      6
      7
      if (inOptions.isDate && list.date != '') {
      try {
      ret.date = moment(list.date).format(config.date_format || 'YYYY-MM-DD')
      } catch(ex) {
      ret.date = moment(list.date._d).format(config.date_format || 'YYYY-MM-DD')
      }
      }