Sivabalan Sivabalan - 1 year ago 146
Javascript Question

TypeError: server.views is not a function in hapi.js

I am a beginner on node.js I am using hapi framework for my web app. In that i am using handlebars for template. When i configure the server views i am getting the type error.

'use strict';

const hapi = require('hapi');
const server = new hapi.Server();
const inert = new require('inert');

host: '',
port: 8080,

//Starting server
server.start((error) => {
throw error;
console.log("Server running" + server);

server.register(inert, () => {
engines: {
html: require('handlebars')
path: 'views',
layoutPath: 'views/layout',
layout: 'default',
partialsPath: 'views/partials'
//helpersPath: 'views/helpers',

I getting this error:

TypeError: server.views is not a function
at server.register (/home/developer/Workspace/kravein-test/app/backend/server.js:22:9)
at process.nextTick (/home/developer/Workspace/kravein-test/node_modules/hoek/lib/index.js:854:22)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:577:11)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3

I am using hapi 15.0.3. Thanks

Answer Source

For making use of server.views, you would need the Vision plugin

e.g. with Handlebars

const server = new Hapi.Server();
server.connection({ port: 8000 });

const handler = function (request, reply) {

    reply.view('basic/index', {
        title: 'examples/views/handlebars/basic.js | Hapi ' + request.server.version,
        message: 'Hello World!'

server.register(require('vision'), (err) => {

    if (err) {
        throw err;

        engines: { html: require('handlebars') },
        path: __dirname + '/templates'

    server.route({ method: 'GET', path: '/', handler: handler });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download