hexo s 报错 FATAL { err: TypeError: line.matchAll is not a function

今天执行 hexo s 启动服务时报如下错误:

1
2
3
4
FATAL { err:
TypeError: line.matchAll is not a function
at res.value.res.value.split.map.line (/Users/ganzhixiong/Documents/blog/node_modules/hexo-util/lib/highlight.js:128:26)
at Array.map (<anonymous>)

网上关于此问题其实没有我讲的详情和清楚,不妨看看我的解题思路。

matchAll is not a function 这个错误其实很明显,意思就是找不到 matchAll 这个函数。
我猜想是当前 Node.js 版本不支持 matchAll 这个函数,下面我们验证下。

  1. 定位到报错代码行列 128:26

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    res.value = res.value.split('\n').map(line => {
    const prepend = tokenStack.map(token => `<span class="${token}">`).join('');
    // 定位到如下行。
    const matches = line.matchAll(/(<span class="(.*?)">|<\/span>)/g);
    for (const match of matches) {
    if (match[0] === '</span>') tokenStack.shift();
    else tokenStack.unshift(match[2]);
    }
    const append = '</span>'.repeat(tokenStack.length);
    return prepend + line + append;
    }).join('\n');
  2. 通过代码可以得知 line 是一个字符串,那么 matchAllstring 的一个函数。

  3. 查看 matchAll 这个特性哪个版本的 Node.js 支持。

    打开 Node.js ES2015/ES6, ES2016 and ES2017 support 搜索 matchAll

    可以看到 String.prototype.matchAll 是从 ES2020 开始支持的,只有 Node.js 版本大于等于 12.0.0 才支持该功能。

    String.prototype.matchAll()
    matchAll() 方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器。

  4. 查看并切换或升级你的 Node.js 版本。

    我使用的是 n 这个工具管理 node 版本,它使用非常简单。用 npm i -g n 全局安装 n,用 n --help 查看如何使用。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ node -v                
    v10.16.3

    $ n
    node/6.9.2
    node/10.16.3
    ο node/12.13.1

    Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

    $ node -v
    v10.16.3

hexo s 报错 FATAL { err: TypeError: line.matchAll is not a function

https://ganzhixiong.com/p/81f9e8f0/

Author

干志雄

Posted on

2021-10-05

Updated on

2021-10-05

Licensed under

Comments