GitBook快速入门

1. GitBook本地安装

(1)安装Node.js

Node.js目前最新LTS版本是14.17.3,但装最新版会报以下错误:

> gitbook -V

TypeError: cb.apply is not a function

运行gitbook init会报以下错误:

> gitbook init
...
warn: no summary file in this book
info: create README.md
info: create SUMMARY.md

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise

一个简易解决方法,安装低版本的Node.js,在Previous Releases | Node.js下载,我安装的是node-v12.22.3-x64.msi

(2)安装gitbook-cli

npm install -g gitbook-cli

2. 创建书籍

使用命令gitbook init初始化一本书:

> gitbook init
warn: no summary file in this book
info: create SUMMARY.md
info: initialization is finished

SUMMARY.md是整本书的目录。

3. 浏览书籍

构建书籍:

gitbook build

使用命令gitbook serve开启gitbook服务器,在浏览器输入http://localhost:4000,即可访问书籍。

(base) PS D:\tmp> gitbook serve
Live reload server started on port: 35729
Press CTRL+C to quit ...

info: 7 plugins are installed
info: loading plugin "livereload"... OK
info: loading plugin "highlight"... OK
info: loading plugin "search"... OK
info: loading plugin "lunr"... OK
info: loading plugin "sharing"... OK
info: loading plugin "fontsettings"... OK
info: loading plugin "theme-default"... OK
info: found 1 pages
info: found 1 asset files
info: >> generation finished with success in 0.4s !

Starting server ...
Serving book on http://localhost:4000

4. 遇到问题及解决

(1)TypeError: cb.apply is not a function

$ gitbook serve
Live reload server started on port: 35729
Press CTRL+C to quit ...

C:\Users\SU\AppData\Roaming\npm\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 C:\Users\SU\AppData\Roaming\npm\node_modules\gitbook-cli\node_modules\npm\node_modules\graceful-fs\polyfills.js:287:18
    at FSReqCallback.oncomplete (fs.js:169:5)

使用nvm降低node.js版本,按上述找到polyfills.js文件,将下列三行注释:

//fs.stat = statFix(fs.stat)
//fs.fstat = statFix(fs.fstat)
//fs.lstat = statFix(fs.lstat)

(2)gitbook build没有任何提示信息

gitbook build没有任何提示信息,运行gitbook serve,只是给出信息Live reload server started on port: 35729,并没有加载插件的信息。

SU@su-laptop MINGW64 /d/docs/working-notes
$ gitbook build

SU@su-laptop MINGW64 /d/docs/working-notes
$ gitbook serve
Live reload server started on port: 35729
Press CTRL+C to quit ...

解决方法:我用nvm管理多个Node.js版本,安装了一个版本为12.12.12的版本(之前用的是12.12.3),再次安装gitbook(npm install -g gitbook-cli),这样就可以用了。

nvm install 12.22.12

nvm use 12.22.12

npm install -g gitbook-cli
本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2023-03-01 23:20

results matching ""

    No results matching ""