Search code examples
babeljskoa

Node.js 6.x async/await in required libs with Babel


For some reason I should use Node.js 6.x (actually 6.10.0) and I'm trying to run koa2 with Babel as described here. My code looks like this

const Koa = require('koa');
var app = new Koa();

// --- middlewares ---
async function a(){await 1} // okay here!
console.log(a());
const serve = require('koa-static');
app.use(serve('./web'));
app.use(require('koa-cookie').default());
app.use(require('koa-bodyparser')()); // fails here

And I'm getting error

D:\Project\node_modules\koa-bodyparser\index.js:72
  return async function bodyParser(ctx, next) {
               ^^^^^^^^
SyntaxError: Unexpected token function

Running node with nodemon index.js --exec babel-node on Windows Works perfect on Node 7.x, but I need to use 6 unfortunately :(


Solution

  • async and await are not yet supported in Node.js 6.x. They are ES2017 features and are only supported in Node 7.6+.

    The Babel preset you're using is obviously not working - make sure you are using the correct babel preset (ES2017) as per the instructions on the Babel site.