Install and Update Ghost as an NPM Module


The ability to install and update Ghost as an NPM module means that someone can easily take an existing node.js app and add Ghost onto it by adding just a few lines to their code.

Below we have instructions on how to install Ghost, update Ghost, and a basic example application.

Installing Ghost

  1. Change to your node app's directory

    cd /path/to/node/app
    
  2. Inside your package.json file, add Ghost as a dependency:

    "dependencies": { 
         "ghost": "latest" 
    }
    
  3. Install Ghost:

    npm install --production
    
  4. Now, inside your node.js app, in the place where you want to run Ghost (ex. app.js), add the following:

    var ghost = require('ghost');
    ghost().then(function (ghostServer) {
        ghostServer.start();
    });
    

Updating Ghost

  1. If you left, change directories back to where your app is placed (the folder where 'node_modules' is)
  2. Run the following command

    npm update ghost
    
  3. And thats it! It will update Ghost to the latest version or whatever version you have specified in your package.json (if you don't see anything in terminal when running the command, it is most likely because you are in the wrong folder).

Example Application

To give you an example of this, we have created an extremely simple node.js app with Ghost being used as an NPM module. Inside a folder, we put 2 files: hello.js (original file taken from http://howtonode.org/hello-node and then edited)

// Load the http module to create an http server.
var http = require('http');

// Load the Ghost Module and Start Ghost
var ghost = require('ghost');
ghost();

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

package.json

{
  "dependencies": {
    "ghost": "latest"
  }
}

Now in this folder, do the following 2 commands:

npm install 
node hello.js

You can now see that the Hello World is being displayed on localhost:8000 and Ghost is running on localhost:2368. Now this is obviously an extremely simple example of Ghost being attached to another node.js app, but hopefully it helps to make things more clear.