Skip to content

[Bug]: export * as name syntax causes babel to fail with unhandled error when using plugin-transform-modules-commonjsΒ #13273

@siddharthvp

Description

@siddharthvp

πŸ’»

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

Consider a 2-file project:

// main.ts
export * as utils from './utils';

// utils.ts
export const num = 42;

Instead writing:

import * as utils from './utils';
export {utils}

works without issues.

Configuration file name

babel.config.js

Configuration

module.exports = {
    ignore: [
        "node_modules/**/*.ts",
        "**/*.d.ts"
    ],
    "presets": ["@babel/preset-typescript"],
    "plugins": [
        [
            "@babel/plugin-transform-modules-commonjs",
            {
                loose: true,
                noInterop: true
            }
        ]
    ]
}

Current and expected behavior

Current behaviour:

TypeError: /path/to/project/main.ts: Cannot read property 'name' of undefined
    at /path/to/project/node_modules/@babel/helper-module-transforms/lib/normalize-and-load-metadata.js:274:51
    at Array.forEach (<anonymous>)
    at /path/to/project/node_modules/@babel/helper-module-transforms/lib/normalize-and-load-metadata.js:273:33
    at Array.forEach (<anonymous>)
    at getLocalExportMetadata (/path/to/project/node_modules/@babel/helper-module-transforms/lib/normalize-and-load-metadata.js:261:27)
    at getModuleMetadata (/path/to/project/node_modules/@babel/helper-module-transforms/lib/normalize-and-load-metadata.js:113:21)
    at normalizeModuleAndLoadMetadata (/path/to/project/node_modules/@babel/helper-module-transforms/lib/normalize-and-load-metadata.js:59:7)
    at rewriteModuleStatementsAndPrepareHeader (/path/to/project/node_modules/@babel/helper-module-transforms/lib/index.js:79:54)
    at PluginPass.exit (/path/to/project/node_modules/@babel/plugin-transform-modules-commonjs/lib/index.js:149:83)
    at newFn (/path/to/project/node_modules/@babel/traverse/lib/visitors.js:175:21) {
  code: 'BABEL_TRANSFORM_ERROR'

Expected behaviour:

Should compile without issue, or at least throw a meaningful error message.

The code compiles without issue with tsc.

Environment

System:
  OS: macOS 11.0
Binaries:
  Node: 15.13.0 - /opt/homebrew/bin/node
  npm: 7.7.6 - /opt/homebrew/bin/npm
npmPackages:
  @babel/cli: ^7.13.16 => 7.13.16 
  @babel/core: ^7.14.0 => 7.14.0 
  @babel/plugin-transform-modules-commonjs: ^7.14.0 => 7.14.0 
  @babel/preset-typescript: ^7.13.0 => 7.13.0 
  eslint: ^7.10.0 => 7.25.0 

Possible solution

No response

Additional context

Small changes to babel.config.js such as removing plugin-transform-modules-commonjs parameters causes babel to fail with a meaningful error message.

Also the issue goes away entirely when not using plugin-transform-modules-commonjs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions