Babel 6没有透露我的代码

时间:2015-12-16 22:40:23

标签: javascript node.js ecmascript-6 babeljs

这是我的代码

require('babel-register')
require('babel-polyfill')

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

// logger

app.use(async (ctx, next) => {
  const start = new Date;
  await next();
  const ms = new Date - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

// response

app.use(ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

使用async/await的Koa v2.0采用的示例。不幸的是它不起作用。我收到以下错误:

app.use(async (ctx, next) => {
SyntaxError: Unexpected token (

这是package.json

{
  "name": "app",
  "version": "0.0.1",
  "scripts": {
    "start": "node index"
  },
  "dependencies": {
    "koa": "^2.0.0-alpha.3"
  },
  "devDependencies": {
    "babel-plugin-transform-async-to-generator": "^6.3.13",
    "babel-polyfill": "^6.3.14",
    "babel-preset-stage-0": "^6.3.13",
    "babel-register": "^6.3.13",
    "eslint": "^1.10.3"
  }
}

这是我的.babelrc

{
  "presets": ["stage-0"],
  "plugins": ["transform-async-to-generator"]
}

1 个答案:

答案 0 :(得分:3)

解决。它还需要包含es2015预设并将钩子移动到另一个文件