文章目录
  1. 1. 原因
  2. 2. 解决方案
    1. 2.1. 降级NodeJS
    2. 2.2. 使用resolutions
  3. 3. 写在文末

终于处理好了hexo本地搜索的问题,但是在这个过程中,由于顺手升级了nodejs,其实产生了新问题的。

问题如下:

1
2
3
4
5
6
7
8
$ hexo -s                              
(node:87224) Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:87224) Warning: Accessing non-existent property 'column' of module exports inside circular dependency
(node:87224) Warning: Accessing non-existent property 'filename' of module exports inside circular dependency
(node:87224) Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency
(node:87224) Warning: Accessing non-existent property 'column' of module exports inside circular dependency
(node:87224) Warning: Accessing non-existent property 'filename' of module exports inside circular dependency

虽说不影响运行,但是很莫名其妙,因为除了环境以外,什么都没动过哎。哪来的循环依赖。

直接上google吧,很顺利的找到了解决方案。我个人倾向于这种: https://www.haoyizebo.com/posts/710984d0/

原因

可以直接看这里 https://github.com/nodejs/node/pull/29935 。但是其实不太重要,只要知道是Hexo以及所依赖的插件造成的问题。通过 npx cross-env NODE_OPTIONS="--trace-warnings" hexo s 命令来启动本地服务,可以查看到调试信息,找到是哪里警告和报错。

然后我们追踪可以发现,其实就是stylus跟nodejs14新合并的pr产生的问题。但是通过google到的文章可以知道,这个问题已经在0.54.8版本修复了,也就是说,应该是有插件依赖了旧版本的stylus。每个人的情况不一样,上面的文章说是出在nib,但我出在cjs。这都不重要,问题找到了,现在就来搞解决方案吧。

总的来说有有两大方案吧:

解决方案

降级NodeJS

这个最直接、干脆、暴力,既然是新的nodejs出的问题,那降级不就好了?

Windows:直接到 https://nodejs.org/download/release/v12.22.7/ 下载安装包,并安装到原有位置即可。

Linux/Mac:安装版本管理器n就可以切换版本了

1
2
3
$ npm install -g n
$ n 12.22.7
$ node --version

检查一下node版本,然后最好重启一下console,不然有缓存,npm还是使用的新版本。问题解决!

使用resolutions

resolutions是什么?简单来说,它是 yarn 提供的统一所有依赖和依赖的依赖和依赖的依赖的依赖……(无限循环下去)的字段。什么?你问我yarn是什么?如果真的懒得去查一下的话,那你就把它理解成npm的替代品。

为什么使用resolutions?因为简单方便,如果要用npm来解决依赖问题,需要新安装插件,更麻烦的配置,更多的理解。而resolutions简单直白,就是大声说出来:“这个插件我只用这个版本!”

那么,在 package.json 里增加 resolutions 来覆盖版本定义:

1
2
3
4
5
...
"resolutions": {
"stylus": "^0.54.8"
}
...

如果没有安装yarn,使用 npm install -g yarn 安装一下,然后执行 yarn install,一切都安静了。

写在文末

这之后一定要记得,都要用 yarn 来管理包,不然resolutions就没有用了。

另外,搜索真的是互联网人的重要能力,大家都保持吧!

♦ 本文固定连接:https://www.gsgundam.com/archive/2021-10-29-hexo-nodejs14-accessing-non-existent-property-issue/

♦ 转载请注明:GSGundam 2021年10月29日发布于 GSGUNDAM砍柴工

♦ 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

♦ 原创不易,如果页面上有适合你的广告,不妨点击一下看看,支持作者。(广告来源:Google Adsense)

♦ 本文总阅读量