I have an error with the new ESLint config File, "eslint.config.js"
Asked Answered
A

1

7

When I try to start linting on a file with the new configuration pattern, I get the following error:

PS C:\Users\test> npx eslint index.js
(node:10036) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

Oops! Something went wrong! :(

ESLint: 8.41.0

C:\Users\test\eslint.config.js:1
export default [
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

This my code for now just to try to get it to work

eslint.config.js

export default [
    {
        rules: {
            semi: "error"
        }
    }
];

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint":"npx eslint index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "eslint": "^8.41.0"
  }
}

When I put "type: 'module'" inside my package.json, it will work of course. But I'm pretty sure this shouldn't be the solution. Does anyone have an idea?

Anthropogenesis answered 25/5, 2023 at 9:40 Comment(3)
"But I'm pretty sure this shouldn't be the solution." — What makes you think that? It's the standard approach to writing ECMAScript module based NPM packages.Onset
It's ok to specify "type: 'module' cause now you are using modern module js, I do not know if it is possible to rename eslint file to eslint.config.mjs and that way I think you would not need to modify package.jsonZabrine
Renaming to eslint.config.mjs worked for meAquinas
J
0

Just as suggested in the question comments, renaming your eslint.config.js file to esconfig.config.mjs is one way to fix it and indeed works if you're still using require instead of import.

Jephthah answered 4/8, 2024 at 14:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.