PositiveGuy
PositiveGuy

Reputation: 20212

Unexpected Token * Koa.js

I'm getting a harmony error when trying to run Koa.

Here is the error after I ran my mocha tests, I get this error at the end:

MacBook-Pro$ mocha test
...projects/search-api/node_modules/koa/lib/application.js:179
function *respond(next) {
         ^
SyntaxError: Unexpected token *
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

enter image description here

Here's my server.js

'use strict';

var supertestKoa = require('supertest-koa-agent');

var app = module.exports = require('koa')(),
    port = process.env.PORT || 3000;

supertestKoa(app);

app.listen(port, function(){
    console.log('Koa app is listening on port' + port);
});

Let me know if you need anything else from me to help troubleshoot this.

Upvotes: 2

Views: 1748

Answers (2)

blackmind
blackmind

Reputation: 1296

This is because of function * in application.js not your node file. function * is a generator function in ES6 (ES2015) http://wiki.ecmascript.org/doku.php?id=harmony%3agenerators. This is the next version of javascript that not all version of browsers/node.js support yet.

There are a couple options here you can transpile that file back to ES5 so that browsers/node.js can understand it. You can also use something like babel to transpile your code back to ES5. You can also set the --harmony flag in node which will allow node to understand and utilize the generator function.

Upvotes: 0

greim
greim

Reputation: 9437

It's failing on the generator function* syntax. Here are a few options to get around this:

  • If you're using node 0.11 or higher, you'll need to enable es6 generators by running with harmony. Typically I do something like this: node --harmony path/to/mocha.
  • Alternatively, you can upgrade to io.js, which supports generators and a few other es6 goodies without needing a runtime flag.
  • One other possibility is running the tests using the babel transpiler. The babel website has detailed instructions how to do that (look for "mocha" on that page). This is probably your best bet if you're still on node 0.10 or lower, which have zero generator support.

Upvotes: 3

Related Questions