Eslint解析错误

问题描述

我刚刚在演示项目中配置了eslint,当我运行eslint时,出现以下错误

我还在学习,所以我迷失于如何最好地解决这个问题。

C:\Users\G-L SYstemS\Desktop\js-dev-env-demo\buildScripts\srcServer.js
  6:20  error    Parse errors in imported module '../webpack.config.dev.js': Unexpected token import (7:41)  import/namespace
  6:20  error    Parse errors in imported module '../webpack.config.dev.js': Unexpected token import (7:41)  import/default
  6:20  warning  Parse errors in imported module '../webpack.config.dev.js': Unexpected token import (7:41)  import/no-named-as-default
  6:20  warning  Parse errors in imported module '../webpack.config.dev.js': Unexpected token import (7:41)  import/no-named-as-default-member

C:\Users\G-L SYstemS\Desktop\js-dev-env-demo\webpack.config.dev.js
  7:41  error  Parsing error: Unexpected token import

✖ 5 problems (3 errors,2 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! javascript-development-environment@1.0.0 lint: `esw webpack.config.* src buildScripts --color`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the javascript-development-environment@1.0.0 lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\G-L SYstemS\AppData\Roaming\npm-cache\_logs\2020-09-24T21_49_41_402Z-debug.log

下面是我的.eslintrc.json文件

{
  "root": true,"extends": [
    "eslint:recommended","plugin:import/errors","plugin:import/warnings"
  ],"parserOptions": {
    "ecmaVersion": 7,"sourceType": "module"
  },"env": {
    "browser": true,"node": true,"mocha": true
  },"rules": {
    "no-console": 1
  }
}

和我的webpack.config.dev.js文件是:

import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import { dirname } from "path";
import { config } from "process";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.Meta.url));

export default {
  mode: "development",devtool: "inline-source-map",target: "web",output: {
    path: path.resolve(__dirname,"dist"),publicPath: "/",filename: "bundle.js",},plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",}),],module: {
    rules: [
      {
        test: /\.js$/,exclude: /node_modules/,use: [
          {
            loader: "babel-loader",options: {
              presets: ["@babel/preset-env"],{
        test: /\.css$/,use: ["style-loader","css-loader"],{
        test: /\.html$/,use: [
          {
            loader: "html-loader",options: { minimize: true },};

有任何解决错误消息的帮助吗?问题可能出在我的配置设置(eslint的版本)上。

解决方法

我最终通过禁用某些规则解决了这些问题。参见下面的新规则:

"parser": "babel-eslint","rules": {
    "no-console": 1,"import/no-unresolved": [0,{ "commonjs": true,"amd": true }],"import/named": 0,"import/no-named-as-default": 0,"import/no-named-as-default-member": 0,"import/namespace": 0,"import/default": 0,"import/export": 0
  }

此外,我在eslintrc.json文件中添加了以下行:

"parser": "babel-eslint"

为ES6类中的特定情况添加“ parserOptions”属性到.eslintrc中已不再足够,因为ESLint当前无法自行对其进行解析。这种情况将引发以下错误:

error Parsing error: Unexpected token = 

解决方案是让ESLint由兼容的解析器解析。只需添加:

"parser": "babel-eslint"

到您的.eslintrc文件并运行

npm install babel-eslint --save-devyarn add -D babel-eslint

这些解决了我的问题。