Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async functions defined on module throw TypeError #202

Closed
amccloud opened this issue Nov 5, 2016 · 7 comments
Closed

Async functions defined on module throw TypeError #202

amccloud opened this issue Nov 5, 2016 · 7 comments

Comments

@amccloud
Copy link
Contributor

amccloud commented Nov 5, 2016

Looks like the babel-runtime alias in the webpack config is causing babel to transpile incorrectly. Any async function defined on the module fails because _regenerator2 is undefined. If the aysnc function is defined within a class, like with getInitialProps, everything works. Without the alias everything appears to work. I'm using [email protected].

pages/index.js
async function test() {
  
}
throws:
TypeError: Cannot read property 'default' of undefined
    at /Users/amccloud/Labs/Auth0/next-auth0/.next/dist/pages/index.js:4:36
    at Object.<anonymous> (/Users/amccloud/Labs/Auth0/next-auth0/.next/dist/pages/index.js:19:2)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at _callee$ (/Users/amccloud/Labs/Auth0/next-auth0/node_modules/next/dist/server/require.js:33:46)
with babel-runtime alias
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.test = undefined;

var test = exports.test = function () {
  var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() { // _regenerator2 undefined
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function test() {
    return _ref.apply(this, arguments);
  };
}();

var _regenerator = require("/Users/amccloud/Labs/Auth0/next-auth0/node_modules/babel-runtime/regenerator");

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require("/Users/amccloud/Labs/Auth0/next-auth0/node_modules/babel-runtime/helpers/asyncToGenerator");

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
without babel-runtime alias
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.test = undefined;

var _regenerator = require("babel-runtime/regenerator");

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var test = exports.test = function () {
  var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function test() {
    return _ref.apply(this, arguments);
  };
}();

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
possibly related issues
@amccloud amccloud changed the title Module level async functions throw TypeError Async functions defined on module throw TypeError Nov 5, 2016
@sedubois
Copy link
Contributor

sedubois commented Nov 5, 2016

Looks like this and #168 are duplicates

@amccloud
Copy link
Contributor Author

amccloud commented Nov 5, 2016

@sedubois possibly. I linked to it at the bottom of my issue. @bs1180 mentioned he thinks his problem might be different.

@bs1180
Copy link

bs1180 commented Nov 5, 2016

@amccloud I think this is likely the root cause of both the problems with template literals and async functions (and no doubt other ES6 functionality that no-one has stumbled over yet). I'll test it against the template literals later, fingers crossed.

@nkzawa
Copy link
Contributor

nkzawa commented Nov 6, 2016

It seems this is the problem of babel or babel-plugin-module-resolver. I reported it to their repo:

https://github.com/tleunen/babel-plugin-module-resolver

This problem doesn't occur when you use Function expression or Arrow function as @sedubois reported in #168

@tleunen
Copy link

tleunen commented Nov 6, 2016

Thank you @nkzawa for reporting the issue. I've fixed the issue and released a v2.3.0. Let me know if you have any other problems.

amccloud added a commit to amccloud/next.js that referenced this issue Nov 6, 2016
@amccloud
Copy link
Contributor Author

amccloud commented Nov 6, 2016

Thanks @tleunen!

@nkzawa 2.3.0 fixes the issue I reported. Fix in PR: #218

@nkzawa
Copy link
Contributor

nkzawa commented Nov 7, 2016

@tleunen @amccloud 😍 thanks for your quick response !

@nkzawa nkzawa closed this as completed Nov 7, 2016
@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants