简体   繁体   English

Next.js - Eslint 不会在开发模式下对任何页面进行 linting(除了 pages/_app.js)

[英]Next.js - Eslint is not linting any pages in dev mode (aside from pages/_app.js)

I'm having problems setting up eslint with Next.js.我在使用 Next.js 设置 eslint 时遇到问题。 It actually properly lints all my files when I run next build , but when I run the application in dev mode ( next ), eslint only actually lints pages/_app.js , and totally ignores all my other files (eg. pages/index.js ).当我运行next build ,它实际上正确地检查了我的所有文件,但是当我以开发模式( next )运行应用程序时,eslint 实际上只检查pages/_app.js ,并且完全忽略了我的所有其他文件(例如pages/_app.js pages/index.js )。

My next.config.js looks like this:我的next.config.js看起来像这样:

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    if (dev) {
      config.module.rules.push({
        test: /\.(j|t)sx?$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
      })
    }
    return config
  },
}

and .eslintrc.js looks like this: .eslintrc.js看起来像这样:

module.exports = {
    "env": {
        "browser": true,
        "es6": true,
        "node": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "react"
    ],
    "rules": {
        "react/prop-types": 0,
        "react/react-in-jsx-scope": 0
    }
};

Is there a sample project somewhere that demonstrates setting up eslint with Next.js?某处是否有示例项目演示如何使用 Next.js 设置 eslint? Eslint is pretty much the standard for any modern JS web application, so I'm surprised to find no mention of eslint in the docs or any of the demo projects. Eslint 几乎是任何现代 JS Web 应用程序的标准,所以我很惊讶地发现文档或任何演示项目中都没有提到 eslint。

Ok I figured out the problem, next.js in dev mode doesn't actually lint any pages until you try to load them in your browser.好的,我找到了问题所在,开发模式下的 next.js 实际上不会对任何页面进行 lint,直到您尝试将它们加载到浏览器中。 So if you have linting errors on pages/index.js , you won't actually see them until you load your homepage in the browser.所以如果你在pages/index.js上有 linting 错误,你实际上不会看到它们,直到你在浏览器中加载你的主页。 https://github.com/zeit/next.js/issues/9904 https://github.com/zeit/next.js/issues/9904

I was actually able to achieve the lint on dev mode using the npm concurrenly package with eslint-watch .我实际上能够使用带有eslint-watch的 npm concurrenly包在开发模式下实现 lint 。

Doing this, my commands looks like (in package.json ) :这样做,我的命令看起来像(在package.json ):

"scripts": {
    "dev": "concurrently \"npm run lint-js:watch\" \"npm run lint-md:watch\" \"next dev\"",
    "build": "next build",
    "start": "next start",
    "lint-md": "remark .",
    "lint-md:watch": "remark --watch .",
    "lint-js": "eslint -c ./.eslintrc --ignore-path ./.eslintignore ./src",
    "lint-js:watch": "esw -w -c ./.eslintrc --ignore-path ./.eslintignore"
  }

Hope that will help!希望这会有所帮助!

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

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