gitbook运行错误解决(cb.apply(this, arguments))
新搞得的阿里云,转移了网站,安装nodejs和gitbook后,生成文件时报错。
一、报错内容
$ gitbook build
Installing GitBook 3.2.3
/usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287
if (cb) cb.apply(this, arguments)
^
TypeError: cb.apply is not a function
at /usr/local/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287:18
at FSReqCallback.oncomplete (node:fs:208:5)
补充(2025-1-26)最新解决办法
- 打开报错的文件
/home/hotpod/.nvm/versions/node/v23.6.1/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js
- 找到调用此函数的位置,并将其注释掉,第62-64行
//fs.lstat = statFix(fs.lstat) //fs.fstat = statFix(fs.fstat) // fs.stat = statFix(fs.stat)
二、nvm重新安装nodejs
nvm可以安装指定版本的nodejs,即可以用来安装低版本的nodejs,我这里用的v10.24.1版本。 这样gitbook使用3.2.3
- 卸载原gitbook
npm uninstall gitbook-cli -g
- 卸载原gitbook
- 安装nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
- 查找低版本nodejs
$ nvm ls-remote v10.23.1 (LTS: Dubnium) v10.23.2 (LTS: Dubnium) v10.23.3 (LTS: Dubnium) v10.24.0 (LTS: Dubnium) v10.24.1 (Latest LTS: Dubnium) v11.0.0 v11.1.0 v11.2.0
- 安装V10版本nodejs
nvm install 10.24.1 Downloading and installing node v10.24.1... Downloading https://nodejs.org/dist/v10.24.1/node-v10.24.1-linux-x64.tar.xz... ################################################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v10.24.1 (npm v6.14.12) Creating default alias: default -> 10.24.1 (-> v10.24.1)
- 重新安装gitbook ~~~ npm install gitbook-cli -g /home/hotpod/.nvm/versions/node/v10.24.1/bin/gitbook -> /home/hotpod/.nvm/versions/node/v10.24.1/lib/node_modules/gitbook-cli/bin/gitbook.js
- gitbook-cli@2.3.2 added 578 packages from 672 contributors in 13.633s ~~~
- 查看gitbook版本
gitbook -V CLI version: 2.3.2 GitBook version: 3.2.3
- 安装gitbook扩展
我的gitbook使用了search-pro, code, expandable-chapters, back-to-top-button, tbfed-pagefooter, toc, theme-comscore扩展,需要在生成前将扩展安装好。
gitbook install
info: installing 7 plugins using npm@3.9.2
info:
info: installing plugin "search-pro"
info: install plugin "search-pro" (*) from NPM with version 2.0.2
/home/hotpod/web/bytetoy
└── gitbook-plugin-search-pro@2.0.2 extraneous
info: >> plugin "search-pro" installed with success
info:
info: installing plugin "code"
runTopLevelLifecycles → 2 ▌ ╢███████████████████████████████████████████████████████████████████████░░░╟
/home/hotpod/web/bytetoy
├── gitbook-plugin-code@0.1.0 extraneous
└── gitbook-plugin-search-pro@2.0.2 extraneous
三、重新生成
运行
gitbook build
重新生成静态文件即可。
gitbook build
info: 14 plugins are installed
info: 11 explicitly listed
info: loading plugin "search-pro"... OK
info: loading plugin "code"... OK
info: loading plugin "expandable-chapters"... OK
info: loading plugin "back-to-top-button"... OK
info: loading plugin "tbfed-pagefooter"... OK
info: loading plugin "toc"... OK
info: loading plugin "highlight"... OK
info: loading plugin "lunr"... OK
info: loading plugin "fontsettings"... OK
info: loading plugin "theme-comscore"... OK
info: loading plugin "theme-default"... OK
info: found 27 pages
info: found 8 asset files
init!
warn: "options" property is deprecated, use config.get(key) instead
finish!
info: >> generation finished with success in 2.5s !