KoaJS v2, BabelJS & npm scripts

Posted on Feb 25, 2016

Koa v2 has changed completely the middleware function signature, used in v1.x, to use ES2016 async/await functions over generators .

It’s still in alpha even thought the Koa’s team says that it’s production ready, because it’s currently used in some systems which are in production, however the issues is that NodeJS doesn't still support async/await function natively , hence you need a transpiler to be able to use async/await functions.

One, and probably the most famous, ES2015 tranpilers which also supports some of the features proposed for ES2016, and between then Async/Await functions, is BabelJS ; it’s also the one, most mentioned, in the Koa’s issue linked above, but it doesn’t look fancy having to transpile server code because it can be very painful, however Koa v2 doesn’t only accept async/await functions as a middleware, it also accepts function which return Promises (which are already supported natively by last productions releases (LTS and no LTS) ) because at the end an async/await function returns a Promise to the caller.

What we can do now

If you don’t want to use BabelJS to write server code because it may be a pain the ass having in your development pipeline, then you can still use Koa v2 using functions which return Promises ; you don’t have to worry in having to rewrite those middlewares again when async/await functions be natively supported by NodeJS, you could use those ones and the new ones that implemented with async/async function seamless.

If you don’t mind to use BabelJS in your server code, because it’s a new development which isn’t going to be a very big codebase, or because the implementation progress is going to be slow for whatever reason or you have any other reason which isn’t relevant for the purpose of this post then go for it and when NodeJS support async/away function natively then kill BabelJS.

Koa v2, BabelJS, with Nodemon & npm scripts

In case that you want to use BabelJS, and you don’t have any prepared boilerplate to develop a server side with Babel with server restarting automatically on each change, then you can follow this steps to install it, configure it and run it with npm.

Assuming that you have ran npm init in a folder and you’re in your terminal on it execute

  1. Install Koa v2 npm install --save koa@next
  2. Install Babel and the presets and plugins needed to transpile ES6/7 features not supported natively by NodeJS; I recommend only use the async/await functions to be able to run your code as soon as async/await function are supported by NodeJS and may not others as spread paramaters, etc. npm install --save-dev babel-core babel-cli babel-preset-es2015-node5 babel-plugin-transform-async-to-generator
  3. Install nodemon npm install --save-dev nodemon

Then setup babel creating a .babelrc file with this content:

{
  "env": {
    "development": {
      "presets": ["es2015-node5"],
      "plugins": ["transform-async-to-generator"]
    }
  }
}

Then you can create, into the property name scripts of package.json, something like this:

{
  "scripts": {
    "dev": "nodemon --exec npm run -s serve",
    "serve": "babel-node -- src/server.js"
  }
}

In case that you use eslint to lint your code, to avoid linter issues with async/await functions you have to install it and babel plugin

npm install --save-dev eslint babel-eslint

then set in .eslintrc file the property "parser": "babel-eslint". Bear in mind that you will have to enable the es6 features too, enabling `es6` environtment .

and you can also setup a watch mode in package.json scripts property

{
  "lint-w": "nodemon --watch src -e js --exec eslint ."
}

then you can run the “dev” script target in one terminal tab and in other this one and enjoy developing a Koa v2 app, with asyn/await function with live restart and live linting.

Don’t wait until Koa v2 to be announced as production version, use it now with Babel or without, just use it now.

Happy coding!