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

Missing module error with the babelification #144

Closed
sindresorhus opened this issue Nov 6, 2015 · 13 comments
Closed

Missing module error with the babelification #144

sindresorhus opened this issue Nov 6, 2015 · 13 comments
Assignees
Labels
bug current functionality does not work as desired priority

Comments

@sindresorhus
Copy link
Member

@floatdrop after 1d5ef4c landed I'm now getting this error when testing got using master.

~/dev/got master
❯ ava

module.js:339
    throw err;
    ^

Error: Cannot find module 'babel-runtime/helpers/async-to-generator'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/sindresorhus/dev/got/test/unix-socket.js:3:25)
    at Module._compile (module.js:435:26)
    at requireFromString (/Users/sindresorhus/dev/ava/node_modules/require-from-string/index.js:14:4)
    at Object.<anonymous> (/Users/sindresorhus/dev/ava/lib/babel.js:28:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
module.js:339
    throw err;
    ^

Error: Cannot find module 'babel-runtime/helpers/async-to-generator'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/sindresorhus/dev/got/test/retry.js:3:25)
    at Module._compile (module.js:435:26)
    at requireFromString (/Users/sindresorhus/dev/ava/node_modules/require-from-string/index.js:14:4)
    at Object.<anonymous> (/Users/sindresorhus/dev/ava/lib/babel.js:28:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
TypeError: Cannot read property 'stack' of undefined
    at error (/Users/sindresorhus/dev/ava/cli.js:51:19)
From previous event:
    at Object.<anonymous> (/Users/sindresorhus/dev/ava/cli.js:218:34)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3
@sindresorhus sindresorhus added the bug current functionality does not work as desired label Nov 6, 2015
@floatdrop floatdrop self-assigned this Nov 6, 2015
@floatdrop
Copy link
Contributor

@sindresorhus babel transpile files with async and prepends

var _asyncToGenerator = require('babel-runtime/helpers/async-to-generator')['default'];

At the top, but in module.paths of test there is no babel-runtime module:

Module {
  id: '/Users/floatdrop/github.com/got/test/unix-socket.js',
  exports: {},
  parent:
   Module {
     id: '/Users/floatdrop/github.com/got/node_modules/ava/node_modules/require-from-string/index.js',
     exports: [Function: requireFromString],
     parent:
      Module {
        id: '.',
        exports: {},
        parent: null,
        filename: '/Users/floatdrop/github.com/got/node_modules/ava/lib/babel.js',
        loaded: false,
        children: [Object],
        paths: [Object] },
     filename: '/Users/floatdrop/github.com/got/node_modules/ava/node_modules/require-from-string/index.js',
     loaded: true,
     children: [ [Circular] ],
     paths:
      [ '/Users/floatdrop/github.com/got/node_modules/ava/node_modules/require-from-string/node_modules',
        '/Users/floatdrop/github.com/got/node_modules/ava/node_modules',
        '/Users/floatdrop/github.com/got/node_modules',
        '/Users/floatdrop/github.com/node_modules',
        '/Users/floatdrop/node_modules',
        '/Users/node_modules',
        '/node_modules' ] },
  filename: '/Users/floatdrop/github.com/got/test/unix-socket.js',
  loaded: false,
  children: [],
  paths:
   [ '/Users/floatdrop/github.com/got/test/node_modules',
     '/Users/floatdrop/github.com/got/node_modules',
     '/Users/floatdrop/github.com/node_modules',
     '/Users/floatdrop/node_modules',
     '/Users/node_modules',
     '/node_modules' ] }

It looks like we should add some paths to paths property, but Node.js behaves the same way. Will look further in require hook for solution.

@floatdrop
Copy link
Contributor

Removing runtime from optional helps (but it breaks Node.js 0.10.x).

@floatdrop
Copy link
Contributor

So including runtime into optional makes Babel place require at the top. Removing it will work for Node.js with generators.

For Node.js 0.10.x we need regenerator runtime, which is depends on global Promise - facebook/regenerator#142 (like co). Babel includes regenerator runtime + core.js polyfills, when used with babel-core/register.

We could append regenerator runtime before test code, but if we append Promise polyfill - it will leak into test code scope, which is bad (but not in module which is good). Or make Promise local to regenerator with function wrapping.

@sindresorhus wdyt?

@sindresorhus
Copy link
Member Author

Or make Promise local to regenerator with function wrapping.

This sounds like the best solution, but I would say we go with the fastest solution now. We can do it properly later. This issue is blocking AVA 0.4.0, so would be nice to get it fixed as fast as possible.

@sindresorhus
Copy link
Member Author

core.js polyfills

Are we including this now? If not, that's a regression too.

@jamestalmage
Copy link
Contributor

Simply installing babel-runtime locally fixed.

It seems to me the requireFromString line is causing the error.

requires in the transpiled code are trying to resolve from the wrong directory to get access to babel-runtime. The resolution is happening from the base directory, but the runtime is in ./node_modules/ava/node_modules/babel-runtime, which won't resolve.

It should be possible to use derequire to change all the require(...) to ___uniquederquirename__ in the transpiled code to else, then prefix the generated code with:

function ___uniquederequirename___(path) {
   try {
      return require(path);
   } catch (e) {
       if (/^babel/.test(path)) {
          // resolve from ava's root directory
       }  else {
           throw e;
       }
   }
}

@sindresorhus sindresorhus added this to the 0.4.0 milestone Nov 7, 2015
@floatdrop
Copy link
Contributor

Are we including this now? If not, that's a regression too.

@sindresorhus all polyfills now excluded from tests, because they affect tested code. I guess fastest way is to revet this commit and use babel/register.

@jamestalmage resolution is happening in right directory, but yes, there is no babel-runtime around, so we either need to append parts of it to code (regenerator and Promise).

@sindresorhus
Copy link
Member Author

all polyfills now excluded from tests, because they affect tested code

With Babel 6 we can use https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime, right?

Externalise references to helpers and builtins, automatically polyfilling your code without polluting globals

@sindresorhus
Copy link
Member Author

Actually, in the Babel 5 docs, it says that the runtime plugin does not pollute globals:

Basically, you can use built-ins such as Promise, Set, Symbol etc as well use all the Babel features that require a polyfill seamlessly, without global pollution, making it extremely suitable for libraries.

https://github.com/babel/babel.github.io/blob/862b43db93e48762671267034a50c30c00e433e2/docs/usage/runtime.md

@floatdrop
Copy link
Contributor

@sindresorhus then it is worth to workaround require somehow:

  • install babel-runtime as peer-dependency
  • hack on m.paths in require-from-string
  • replace runtime require path with resolved one

@sindresorhus
Copy link
Member Author

install babel-runtime as peer-dependency

Are you sure you meant peerDependency? They don't install anything, but rather only warns the user to manually install it. I don't think we'd want that.

@floatdrop
Copy link
Contributor

@sindresorhus yeah. I think I give a m.paths a shot, because I don't want to mess with replacing require statements.

@sindresorhus
Copy link
Member Author

Agreed.

floatdrop added a commit to floatdrop/ava that referenced this issue Nov 7, 2015
floatdrop added a commit to floatdrop/ava that referenced this issue Nov 7, 2015
floatdrop added a commit to floatdrop/ava that referenced this issue Nov 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug current functionality does not work as desired priority
Projects
None yet
Development

No branches or pull requests

3 participants