1

I cannot make express router work with async/await functions with Babel v6.

It throws such error:

MacBook-Pro-Dmitri:api dmitri$ npm start

> dhhb-api@0.1.0 start /Users/dmitri/github/dhhb/api
> node bin/runServer

/Users/dmitri/github/dhhb/api/node_modules/express/lib/router/route.js:196
        throw new Error(msg);
              ^
Error: Route.post() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as post] (/Users/dmitri/github/dhhb/api/node_modules/express/lib/router/route.js:196:15)
    at Function.proto.(anonymous function) (/Users/dmitri/github/dhhb/api/node_modules/express/lib/router/index.js:510:19)
    at exports.default (index.js:41:13)
    at exports.default (index.js:13:28)
    at Object.<anonymous> (server.js:27:16)
    at Module._compile (module.js:460:26)
    at loader (/Users/dmitri/github/dhhb/api/node_modules/babel-core/node_modules/babel-register/lib/node.js:130:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/dmitri/github/dhhb/api/node_modules/babel-core/node_modules/babel-register/lib/node.js:140:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/dmitri/github/dhhb/api/bin/runServer.js:4:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)   

My .babelrc:

{
  "presets": [
    "es2015",
    "stage-0"
  ],
  "plugins": [
    "syntax-async-functions",
    "transform-regenerator",
    "transform-runtime"
  ]
}

Init file:

require('babel-core/register');
require('babel-polyfill');
require('../src/server.js');

And server js endpoints:

// uncomment to throw 
// app.get('/test1', async function () { // will throw });
app.get('/test2', function () { // no error thrown });

UPDATE

I've setup a demo repo to replicate buggy behavior:

https://github.com/voronianski/babel-express-async-bug

Kosmetika
  • 20,774
  • 37
  • 108
  • 172
  • http://stackoverflow.com/questions/29207878/requirebabel-register-doesnt-work also, if you're trying to use generators in the test 2, make sure you have function *() { .... yield async call ...} – Saad Dec 12 '15 at 19:29
  • @Saad babel-core/register compiles modules on the fly (but of course not the module it is located in)... and I'm talking about async/await functions not generators.. – Kosmetika Dec 12 '15 at 19:32
  • Since when are express callbacks allowed to return promises? – Bergi Dec 12 '15 at 19:48
  • @Bergi it works with Babel v5 – Kosmetika Dec 12 '15 at 19:49
  • @Bergi check this example - https://github.com/voronianski/express-api-sample/blob/master/src/v1/endpoints/user/index.js – Kosmetika Dec 12 '15 at 19:49
  • @Kosmetika: Ah, it doesn't actually, you still have to call `next`. And it doesn't seem to matter for this question anyway. – Bergi Dec 12 '15 at 19:53
  • @Kosmetika: Does it work without the `babel-core/register`, if you compile it manually? Can you show us what it does compile to? – Bergi Dec 12 '15 at 19:54
  • @Bergi I've setup a git repo trying to replicate this behavior - https://github.com/voronianski/babel-express-async-bug – Kosmetika Dec 12 '15 at 20:16
  • @Bergi for now with commented off `async` function it fails strangely with `/Users/dmitri/github/babel-express-router-bug/node_modules/babel-runtime/helpers/typeof.js:14 mbol" : typeof obj === "undefined" ? "undefined" : (0, _typeof3.default)(obj); ^ TypeError: object is not a function` – Kosmetika Dec 12 '15 at 20:18
  • @Bergi just reproduced the bug here github.com/voronianski/babel-express-async-bug – Kosmetika Dec 12 '15 at 20:24
  • @Bergi I've compiled it manually and it behaves similarly (throws same error), the code is available here - https://gist.github.com/voronianski/c41c7c24791769e79b4b – Kosmetika Dec 12 '15 at 20:31
  • @Bergi I see the problem in transpiled code, `var asyncAwait` is not hoisted and is passed to express router.. – Kosmetika Dec 12 '15 at 20:37
  • 1
    @Kosmetika: A problem with `….default` code that worked back with babel 5 sounds being related to [Babel 6 changes how it exports default](http://stackoverflow.com/q/33505992/1048572) – Bergi Dec 12 '15 at 21:20
  • @Kosmetika: OK, that `asnycAwait` transpilation looks like a clear bug. Did you report it already? – Bergi Dec 12 '15 at 21:23
  • 1
    @Bergi yep, reported at https://phabricator.babeljs.io/T6815 – Kosmetika Dec 12 '15 at 21:28

0 Answers0