Search code examples
javascriptnode.jskoa

Why do I get an error "app.use() requires a generator function" using koa-views middleware?


I installed koa, then installed koa-views. After that I copied example code from the koa-views docs and pasted it into index.js

'use strict';

const koa = require('koa');
const app = koa();

var views = require('koa-views');

// Must be used before any router is used
app.use(views(__dirname + '/views', {
    map: {
        html: 'underscore'
    }
}));
app.use(function* (next) {
    this.state = {
        session: this.session,
        title: 'app'
    };
    yield this.render('user', {
        user: 'John'
    });
});

When I try to run this code with command node index.js I get an error:

enter image description here

What's the problem? Why doesn't it work?

p.s.

node version: v6.9.5; 
koa version: 1.2.5; 
koa-views version: 5.2.0

Solution

  • Try installing npm install koa-views@4.1.0 seems like there might be something different with the new version. See here for where I got the solution.

    I tried it, and I no longer got the error.