next.js: Error during build: Cannot read property 'file' of undefined

I’m stuck on an odd error during build, where a certain source file is unable to be transpiled. The file in question just exports a named class: export class ...

The issue seems to be caused by styled-jsx and its moduleExportsVisitor. The file where the error occurs does not use React or styled-jsx.

Any advice/help would be appreciated. For now, I’m working around it by creating a separate build for that module without going through the Next build pipeline. Here’s the stack trace:

Module build failed: TypeError: ~/dev/mooz/vex/src/accidental.js: Cannot read property 'file' of undefined
    at NodePath.getSource (~/dev/node_modules/babel-traverse/lib/path/introspection.js:193:20)
    at moduleExportsVisitor (~/dev/node_modules/styled-jsx/dist/babel-external.js:171:24)
    at callExternalVisitor (~/dev/node_modules/styled-jsx/dist/babel.js:346:3)
    at PluginPass.AssignmentExpression (~/dev/node_modules/styled-jsx/dist/babel.js:308:9)
    at newFn (~/dev/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (~/dev/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (~/dev/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (~/dev/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (~/dev/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (~/dev/node_modules/babel-traverse/lib/context.js:108:19)
    at TraversalContext.visit (~/dev/node_modules/babel-traverse/lib/context.js:192:19)
    at Function.traverse.node (~/dev/node_modules/babel-traverse/lib/index.js:114:17)
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

The build task completes successfully.

Current Behavior

The build task fails with an error mentioned in summary.

Steps to Reproduce

Unknown

Your Environment

Tech Version
next @beta

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Yes I noticed that mistake… It works when passing parentPath.

If I add if (!path.get('object').node) return then all tests pass except 1.

It’s not urgent, as I found a workaround for now in the problematic source file - to declare the variable before the for loop - so it compiles.

Thank you for looking into it, I’m a big fan of Zeit and Next.js.

Ah OK, I didn’t know what I was doing there. So I did as you suggested: replace AssignmentExpression with MemberExpression in babel.js, and then modified moduleExportsVisitor in babel-external.js to the following:

function moduleExportsVisitor(path, opts) {
  if (path.get('object').node.name !== 'module') {
    return
  }
  if (path.get('property').node.name !== 'exports') {
    return
  }
  console.log('yep module export')
  const parentPath = path.parentPath
  defaultExports(path, parentPath, opts)
}

Yes, the build completes successfully. I also tried defining module.exports in the problematic snippet, and it recognizes it.

OK, I will see if I can create a failing test case for styled-jsx.