【问题标题】:How do I get ESLint to scan all files (recursively) from the root?如何让 ESLint 从根目录扫描所有文件(递归)?
【发布时间】:2020-08-04 03:23:44
【问题描述】:

我正在学习关于全栈 Web 开发的课程。课程有a section on ESLint,它建议你可以运行ESLint并使用命令从根目录递归扫描所有文件和目录

./node_modules/.bin/eslint .

但是当我这样做时会出错

Oops! Something went wrong! :(

ESLint: 7.6.0

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at assertPath (path.js:39:11)
    at Object.join (path.js:1157:7)
    at FileEnumerator._iterateFilesRecursive (/Users/David/iPhone/Full Stack Course/Exercises Backend/part3/node_modules/eslint/lib/cli-engine/file-enumerator.js:426:35)
    at _iterateFilesRecursive.next (<anonymous>)
    at FileEnumerator.iterateFiles (/Users/David/iPhone/Full Stack Course/Exercises Backend/part3/node_modules/eslint/lib/cli-engine/file-enumerator.js:287:49)
    at iterateFiles.next (<anonymous>)
    at CLIEngine.executeOnFiles (/Users/David/iPhone/Full Stack Course/Exercises Backend/part3/node_modules/eslint/lib/cli-engine/cli-engine.js:751:48)
    at ESLint.lintFiles (/Users/David/iPhone/Full Stack Course/Exercises Backend/part3/node_modules/eslint/lib/eslint/eslint.js:515:23)
    at Object.execute (/Users/David/iPhone/Full Stack Course/Exercises Backend/part3/node_modules/eslint/lib/cli.js:294:36)
    at main (/Users/David/iPhone/Full Stack Course/Exercises Backend/part3/node_modules/eslint/bin/eslint.js:142:52)

我已经尝试了一切试图让它发挥作用。我可以像这样在特定文件等上成功运行 eslint

./node_modules/.bin/eslint index.js    //Successfully scans index.js
npx eslint model/**   //Scans all files in the model directory
npx eslint *.js  //Scans all .js files at the root but not directories 

但是我不能让它从根递归扫描。这是我尝试过的东西的列表,以及无数其他的变体

npx eslint ./
npx eslint ./**
npx eslint "./**"
npx eslint models/../**

甚至尝试在另一台机器上安装和执行此操作,同样的问题,所以我只能断定命令是错误的。如果有人有任何建议,我将不胜感激!

这是根目录的“ls -a”,以防万一:

.           .eslintrc.js        build           package-lock.json
..          .git            index.js        package.json
.DS_Store       .gitignore      models          requests
.env            Procfile        mongo.js
.eslintignore       README.md       node_modules

编辑: 这是我的 .eslintrc.js 文件的内容

module.exports = {
    'env': {
        'browser': true,
        'es2020': true
    },
    'extends': 'eslint:recommended',
    'parserOptions': {
        'ecmaVersion': 11,
        'sourceType': 'module'
    },
    'rules': {
        'eqeqeq': 'error',
        'no-trailing-spaces': 'error',
        'object-curly-spacing': [
            'error', 'always'
        ],
        'arrow-spacing': [
            'error', { 'before': true, 'after': true }
        ],
        'no-console': 0,
        'indent': [
            'error',
            2
        ],
        'linebreak-style': [
            'error',
            'unix'
        ],
        'quotes': [
            'error',
            'single'
        ],
        'semi': [
            'error',
            'never'
        ]
    }
}

在 .eslintignore 文件中,我只是拥有

build

package.json 文件

{
  "name": "backend",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "build:ui": "rm -rf build && cd ../../part2/1_rendering_collection && npm run build --prod && cp -r build ../../part3/backend/",
    "deploy": "git push heroku master",
    "deploy:full": "npm run build:ui && git add . && git commit -m uibuild && npm run deploy",
    "logs:prod": "heroku logs --tail",
    "lint": "eslint ."
  },
  "author": "David Blake",
  "license": "MIT",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.9.26"
  },
  "devDependencies": {
    "eslint": "^7.5.0",
    "nodemon": "^2.0.4"
  }
}

【问题讨论】:

  • 该命令是正确的,它是官方文档中描述的,它对我有用。你的.eslintrc.js 文件里面有什么?也许那里或您的package.json 中的路径配置不正确?
  • 嗨!我已经用两个文件的内容编辑了这个问题。我在另一台机器上也遇到了同样的错误,安装了 'npx create-react-app dirname' 然后 'npm install eslint --save-dev' 。
  • 您真的要检查 node_modules 中的所有文件吗?!?
  • 不是真的!我只想获得任何可以在文件上运行 eslint 的命令(这是使用 eslint 格式化文件的练习之一的要求)。我想我可以将所有内容移动到“src”目录并尝试 src/** ...但如果 .命令有效

标签: reactjs eslint


【解决方案1】:

我修好了!原来问题是我运行的是旧版本的 Node (v10.5)。更新到 V12,现在一切正常。

这里是guide 阅读此内容的任何人的更新方法。

导致我找到解决方案的原因是我在运行 Jest 时遇到了其他问题

Test suite failed to run
    TypeError: (0 , _vm(...).compileFunction) is not a function

这让我看到了一篇说要更新 Node.js 的帖子。更新后我发现它解决了我的两个问题。因此,如果有人对 Jest 也有类似的问题 - 试试吧

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2013-06-25
    • 2014-03-26
    • 2012-04-02
    • 2019-06-07
    相关资源
    最近更新 更多