当我尝试使用新的配置模式开始对文件进行 linting 时,出现以下错误:
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中时,它当然会起作用。但我很确定这不应该是解决方案。有人有想法吗?
正如问题评论中所建议的,将 eslint.config.js 文件重命名为
esconfig.config.mjs
是修复此问题的一种方法,如果您仍在使用 require
而不是 import
,则确实有效。