question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: [flat config] `.mjs` extension for config file

See original GitHub issue

Environment

Environment Info:

Node version: v19.1.0 npm version: v8.19.3 Local ESLint version: v8.28.0 (Currently used) Global ESLint version: Not found Operating System: darwin 22.1.0

What parser are you using?

Default (Espree)

What did you do?

My npm package is commonjs, with .js extensions, alongside ESM with .mjs extensions for running tests. Running npx eslint . results in Node failing to parse the configuration file, as it assumes it to be commonjs.

What did you expect to happen?

The config file should only support a .mjs extension (eslint.config.mjs) to avoid this problem since Node will always execute it as ESM regardless of the type field defined in package.json.

What actually happened?

(Shortened)

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
...
eslint.config.js:1
export default [
^^^^^^

SyntaxError: Unexpected token 'export'

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:5
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
jaydensericcommented, Nov 30, 2022

If your project does not have a package type field of "module", and your ESM is in .mjs files because .js is in CJS mode, and if you need to use a pure ESM dev dependency within the project ESLint config file, you are screwed. The eslint.config.js file is in CJS mode, meaning you can’t require the ESM dev dependency. ESLint must add support for eslint.config.mjs files.

If in the future ESLint decides to support only one config file name and format that any project could use, it would be eslint.config.mjs because that will always work as ESM no matter what the package type field is configured to, and the ESM format is capable of importing from both ESM as well as CJS dev dependencies.

1reaction
dburlescommented, Dec 5, 2022

@dburles you’ll need to use the environment variable to force the file to be interpreted as flat config.

Whoops! that does it, thanks. That’s a decent workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6 Test Match in configuration file ignoring .mjs extension ...
If you add "mjs" to moduleFileExtensions config, then the mjs file extension will be matched. { verbose: true, projects: [ { displayName: " ......
Read more >
ESLint's new config system, Part 2: Introduction to flat config
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
How to correctly configure the parser & plugins with ESLint's ...
Im using flat config with typescript. Heres what I think are the important parts of the config:
Read more >
K13408: Overview of single configuration files (11.x - 15.x)
A single configuration file (SCF) is a flat, text file that contains all of the objects that compose the BIG-IP configuration.
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
mjs extension. Files with a .js extension when the nearest parent package.json file contains a top-level "type" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found