简体   繁体   English

Eslint 解析错误

[英]Eslint parse errors

I just configured eslint in my demo project and when I run eslint, I get below errors:我刚刚在我的演示项目中配置了 eslint,当我运行 eslint 时,出现以下错误:

I am still learning so I am lost on how best to resolve this.我仍在学习,所以我不知道如何最好地解决这个问题。

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

Below is my .eslintrc.json file:下面是我的 .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
  }
}

and my webpack.config.dev.js file is:我的 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 },
          },
        ],
      },
    ],
  },
};

Any help to resolve the error messages?任何帮助解决错误消息? Could the problem be with my configuration settings, version of eslint.问题可能出在我的配置设置、eslint 版本上吗?

I eventually resolved the problems by disabling some of the rules.我最终通过禁用一些规则解决了这些问题。 See new rules below:请参阅以下新规则:

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

I addition, I added the following line to my eslintrc.json file:另外,我在 eslintrc.json 文件中添加了以下行:

"parser": "babel-eslint"

Adding the "parserOptions" property to your .eslintrc is no longer enough for particular situations in ES6 classes as ESLint is currently unable to parse it on its own.将“parserOptions”属性添加到您的 .eslintrc 已不再适用于 ES6 类中的特定情况,因为 ESLint 目前无法自行解析它。 Such situations will throw an error of:这种情况会抛出以下错误:

error Parsing error: Unexpected token = 

The solution is to have ESLint parsed by a compatible parser.解决方案是让 ESLint 由兼容的解析器解析。 just add:只需添加:

"parser": "babel-eslint"

to your .eslintrc file and run到您的 .eslintrc 文件并运行

npm install babel-eslint --save-dev or yarn add -D babel-eslint . npm install babel-eslint --save-devyarn add -D babel-eslint

These resolved my issues.这些解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM