Skip to content

[BUG] When sourceType is unspecified then babel gives "Parsing error" with ESLint 9 flat config on '.cjs' files #17347

@stephen-carter-at-sf

Description

@stephen-carter-at-sf

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

You can create any dummy.cjs file and run npx eslint . on it and you'll get the following error:

  0:0  error  Parsing error: .sourceType must be "module", "script", "unambiguous", or undefined

This happens when you do not specify a sourceType in your eslint flat config file under languageOptions>parserOptions.

Configuration file name

No response

Configuration

My eslint.config.mjs file looks like:

import babelParser from "@babel/eslint-parser";

export default [
    {
        rules: {
            'no-unassigned-vars': 'error'
        },
        languageOptions: {
            parser: babelParser,
            parserOptions: {
                requireConfigFile: false,
            }
        }
    }
]

Current and expected behavior

Simple repro steps:

  mkdir repro
  cd repro
  npm init
  npm install eslint
  npm install @babel/eslint-parser
  touch dummy.cjs
  echo "let a;\nif(a === 'ready') console.log('ok');" > dummy.cjs
  npx eslint dummy.cjs

and then create a eslint.config.mjs with the above contents shown above.

Then run

npx eslint dummy.cjs

Environment

~/temp/repro: npm --version  
10.9.2
~/temp/repro: node --version  
v23.6.1
~/temp/repro: sw_vers         
ProductName:		macOS
ProductVersion:		15.5
BuildVersion:		24F74
~/temp/repro: cat package.json
{
  "name": "repro",
  "dependencies": {
    "@babel/eslint-parser": "^7.27.1",
    "eslint": "^9.27.0"
  }
}

Possible solution

If I alter my eslint.config.mjs config file to have sourceType: undefined, in the parserOptions like so:

import babelParser from "@babel/eslint-parser";

export default [
    {
        rules: {
            'no-unassigned-vars': 'error'
        },
        languageOptions: {
            parser: babelParser,
            parserOptions: {
                sourceType: undefined,  // Magically fixes the issue
                requireConfigFile: false,
            }
        }
    }
]

then I then get the correct eslint evaluation output without the parser error.

Doing some debugging, I noticed that if I modify the code over at https://github.com/babel/babel/blob/main/packages/babel-core/src/config/validation/option-assertions.ts#L132
to instead spit out the value:

      `${msg(loc)} must be "module", "script", "unambiguous", or undefined. Value was "${value}"`,

then when I go to my original repro steps (without sourceType specified) then I get:

  0:0  error  Parsing error: .sourceType must be "module", "script", "unambiguous", or undefined. Value was "commonjs"

and doing some reading I believe the ESLint 9 now automatically fills in the sourceType depending on the file extension if it isn't specified.

I believe the solution is that babel needs to map "commonjs" or whatever else ESLint auto fills in with to its corresponding "script", "module", or "unambiguous" values.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: eslintoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions