---
title: "GitBook 安装使用，及遇到的坑【Mac下】"
author: "Uncle.Code"
date: "2016-05-12T16:42:09Z"
tags: []
categories: ["开源软件"]
slug: "gitbook-mac"
draft: false
lastmod: "2016-05-13T19:11:03Z"
id: 2297787
url: "https://uncle-code.iteye.com/blog/2297787"
---

1、安装gitbook命令行

```

$ npm install -g gitbook-cli

```

2、查看是否安装成功 

```

$ gitbook -V 

```

3、开始使用
（1）初始化

```

$ mkdir test_gitbook
$ cd test_gitbook
$ touch README.md
$ touch SUMMARY.md
$ vi SUMMARY.md
* [简介](README.md)
* [第一章](chapter1/README.md)
 - [第一节](chapter1/section1.md)
 - [第二节](chapter1/section2.md)
* [第二章](chapter2/README.md)
 - [第一节](chapter2/section1.md)
 - [第二节](chapter2/section2.md)
* [结束](end/README.md)

```

（2）生成目录结构

```

$ gitbook init

```

（3）查看目录结构

```

$ tree
.
├── README.md
├── SUMMARY.md
├── chapter1
│   ├── README.md
│   ├── section1.md
│   └── section2.md
├── chapter2
│   ├── README.md
│   ├── section1.md
│   └── section2.md
└── end
    └── README.md

```

（4）本地预览

```

$ gitbook serve .

```


这一步可能会报错

```

(node:12474) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.
(node) v8::ObjectTemplate::Set() with non-primitive values is deprecated
(node) and will stop working in the next major release.

```


查看node版本

```

$ node -v
v6.0.0

```


原因是：graceful-fs 在 node V6 下不支持，需要降级

```

$ brew tap homebrew/versions
$ brew unlink node
$ brew install homebrew/versions/node5
$ node -v
v5.11.1

```


PS：如果需要使用node 6.0

```

brew unlink homebrew/versions/node5
brew link node

```

切回5.11

```

brew unlink node
brew link homebrew/versions/node5

```


好了，继续执行

```

$ gitbook serve .
Live reload server started on port: 35729
Press CTRL+C to quit ...

info: loading book configuration....OK 
info: load plugin gitbook-plugin-highlight ....OK 
info: load plugin gitbook-plugin-search ....OK 
info: load plugin gitbook-plugin-sharing ....OK 
info: load plugin gitbook-plugin-fontsettings ....OK 
info: load plugin gitbook-plugin-livereload ....OK 
info: >> 5 plugins loaded 
info: start generation with website generator 
info: clean website generatorOK 
info: generation is finished 

Starting server ...
Serving book on http://localhost:4000

```


浏览器打开 http://localhost:4000
可以看到效果啦

这时可以看到当前目录下多了个文件夹
_book 
这个文件里面是静态网站内容

生成静态网站到指定目录

```

gitbook build . ./mysite

```


（5）gitbook 常用命令

```

$ gitbook -h
Usage: gitbook [options] [command]
Commands:
build [options] [source_dir] 编译指定目录，输出Web格式(_book文件夹中)
serve [options] [source_dir] 监听文件变化并编译指定目录，同时会创建一个服务器用于预览Web
pdf [options] [source_dir] 编译指定目录，输出PDF
epub [options] [source_dir] 编译指定目录，输出epub
mobi [options] [source_dir] 编译指定目录，输出mobi
init [source_dir]   通过SUMMARY.md生成作品目录
Options:
-h, --help     output usage information
-V, --version  output the version number

```

（6）gitbook 生成pdf

```

$ gitbook pdf . mypdf.pdf
Error: Need to install ebook-convert from Calibre

```

这时候去下载Calibre
http://calibre-ebook.com/download_osx
安装完成后

```

ln -s /Applications/calibre.app/Contents/MacOS/ebook-convert /usr/local/bin

```

再次执行

```

$ gitbook pdf . mypdf.pdf

```

会在当前目录下生成 mypdf.pdf
（7）使用 gitbook editor 制作电子书
下载地址：https://www.gitbook.com/editor/

这个就不再赘述了。