How to serve a Gitbook on Heroku How to serve a Gitbook on Heroku heroku heroku

How to serve a Gitbook on Heroku


Do not run the gitbook-cli directly on Heroku, gitbbok will use a lot of memory and pass the 512MB limit for a free dyno. The best way is to first build your book to a static website and then serve the static content with either nodejs with express static or with sinatra.

Run the following inside your gitbook folder.

gitbook build

A new folder will be created, _book and all your html files will be here. You can now create a new app on Heroku and serve your static content.

After you do gitbook build, you can serve that directory with Express by creating an app.js file like this:

var express = require('express'); var app = express();app.use(express.static(__dirname + '/_book'));app.listen(process.env.PORT || 3000);

Dead-easy. For more information: Static files served by node.js on heroku - is it a good idea?


1- Add gitbook-cli to your project:

npm install --save gitbook-cli

2- Add a Procfile to your project:

web: ./node_modules/gitbook-cli/bin/gitbook.js serve --port $PORT