Hexo升级插件版本解决兼容性问题

为了精简hexo安装的npm modules,用rm -rf node_modules/删除了已安装的包,再npm install把package.json中定义的包安装一遍。结果hexo自动从4.0.0升级到了4.2.1,hexo g时失败。node.js除了hexo外基本不用,npm自然不熟,有些配置是直接抄来的并不知其所以然。而hexo的插件都是由npm管理的,稍微研究了下如何查看这些插件的最新版本和升级它们。

hexo g失败错误

错误信息如下:

$ hexo d -g
INFO  Start processing
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
TypeError: Cannot read property 'length' of undefined
    at module.exports (/home/finisky/node_modules/hexo-related-popular-posts/lib/cache.js:26:77)
    at Hexo.hexo.extend.filter.register (/home/finisky/node_modules/hexo-related-popular-posts/index.js:171:34)
    at Hexo.tryCatcher (/home/finisky/node_modules/bluebird/js/release/util.js:16:23)
    at Hexo.<anonymous> (/home/finisky/node_modules/bluebird/js/release/method.js:15:34)
    at Promise.each.filter (/home/finisky/node_modules/hexo/lib/extend/filter.js:62:52)
    at tryCatcher (/home/finisky/node_modules/bluebird/js/release/util.js:16:23)
    at Object.gotValue (/home/finisky/node_modules/bluebird/js/release/reduce.js:166:18)
    at Object.gotAccum (/home/finisky/node_modules/bluebird/js/release/reduce.js:155:25)
    at Object.tryCatcher (/home/finisky/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/finisky/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/home/finisky/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromiseCtx (/home/finisky/node_modules/bluebird/js/release/promise.js:641:10)
    at _drainQueueStep (/home/finisky/node_modules/bluebird/js/release/async.js:97:12)
    at _drainQueue (/home/finisky/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/home/finisky/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues (/home/finisky/node_modules/bluebird/js/release/async.js:15:14)
    at runCallback (timers.js:794:20)
    at tryOnImmediate (timers.js:752:5)
    at processImmediate [as _immediateCallback] (timers.js:729:5)

看起来像是hexo-related-popular-posts插件中的一个变量未定义,搜了一下,还是因为hexo升级后有些变量修改了位置导致变量不存在。当然可以通过修改源码的方式来解决问题,不过更好的做法是升级这些包到最新版本。

查看package最新版本

想升级版本,首先得知道这些包的最新版本是什么。

npm outdated可以用来查看已安装包的最新可用版本,可作为升级版本的参考,文档如下: https://docs.npmjs.com/cli-commands/outdated.html

$ npm outdated
Package                     Current  Wanted  Latest  Location
hexo-related-popular-posts    4.0.0   3.0.6   4.0.0  hexo-site
hexo-deployer-git             1.0.0   1.0.0   2.1.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-sitemap        1.2.0   1.2.0   2.0.0  hexo-site
hexo-generator-tag            0.2.0   0.2.0   1.0.0  hexo-site
hexo-server                   0.3.3   0.3.3   1.0.0  hexo-site

一看hexo-related-popular-posts已经被标红了,Current版本已经高于了Wanted,出现兼容性问题并不意外。标红的含义:

  • Red means there’s a newer version matching your semver requirements, so you should update now.
  • Yellow indicates that there’s a newer version above your semver requirements (usually new major, or new 0.x minor) so proceed with caution.

修改package.json

知道了最新版本是什么,看看当前package.json的配置:

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "hexo": {
    "version": "4.0.0"
  },
  "dependencies": {
    "constantinople": "^4.0.1",
    "hexo": "^4.0.0",
    "hexo-deployer-git": "^1.0.0",
    "hexo-generator-archive": "^0.1.5",
    "hexo-generator-baidu-sitemap": "^0.1.6",
    "hexo-generator-category": "^0.1.3",
    "hexo-generator-index": "^0.2.1",
    "hexo-generator-sitemap": "^1.2.0",
    "hexo-generator-tag": "^0.2.0",
    "hexo-lazyload-image": "^1.0.4",
    "hexo-related-popular-posts": "^3.0.6",
    "hexo-renderer-kramed": "^0.1.4",
    "hexo-renderer-stylus": "^1.1.0",
    "hexo-server": "^0.3.1"
  }
}

版本号前的“^”又是什么意思?参考:https://github.com/npm/node-semver#ranges

Caret Ranges ^1.2.3 ^0.2.5 ^0.0.4

Allows changes that do not modify the left-most non-zero element in the [major, minor, patch] tuple. In other words, this allows patch and minor updates for versions 1.0.0 and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X.

Many authors treat a 0.x version as if the x were the major "breaking-change" indicator.

“^”代表最左的非0版本号不会自动升级,因为它可能代表一个breaking change。

好了,升级hexo到4.2.1,升级hexo-related-popular-posts到4.0.0即解决了报错问题,顺手把其他插件也都升级了:

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "hexo": {
    "version": "4.2.1"
  },
  "dependencies": {
    "constantinople": "^4.0.1",
    "hexo": "^4.2.1",
    "hexo-deployer-git": "^2.1.0",
    "hexo-generator-archive": "^1.0.0",
    "hexo-generator-baidu-sitemap": "^0.1.6",
    "hexo-generator-category": "^1.0.0",
    "hexo-generator-index": "^1.0.0",
    "hexo-generator-sitemap": "^2.0.0",
    "hexo-generator-tag": "^1.0.0",
    "hexo-lazyload-image": "^1.0.4",
    "hexo-related-popular-posts": "^4.0.0",
    "hexo-renderer-kramed": "^0.1.4",
    "hexo-renderer-stylus": "^1.1.0",
    "hexo-server": "^1.0.0"
  }
}