You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新ESLint配置文件eslint.config.js报错求助

ESLint新配置模式报错:Unexpected token 'export' 替代解决方案

报错信息

运行npx eslint index.js时出现以下错误:

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)

当前配置文件

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"
  }
}

已知添加"type": "module"到package.json可以解决问题,但不想采用该方案,求其他可行办法。

替代解决方案

方案1:重命名配置文件为eslint.config.mjs

直接将eslint.config.js改名为eslint.config.mjs,Node.js会自动识别这个后缀为ES模块,无需修改package.json,运行lint命令即可正常工作。

方案2:改用CommonJS格式编写配置

把ES模块的export default替换为CommonJS的module.exports,修改后的eslint.config.js如下:

module.exports = [
    {
        rules: {
            semi: "error"
        }
    }
];

这样Node.js会以CommonJS模块加载配置文件,同样不需要修改package.json。


内容的提问来源于stack exchange,提问作者Andy Schunke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:57:06