typescript - ESLint: Parsningsfel: Kan inte läsa filen tsconfig.json i WebStorm IDE på WSL2


När jag klonar mitt repo och kör npm install på WSL2 får jag detta ESLint-fel i min IDE för alla .ts-filer i mitt projekt:

ESLint: Parsingfel: Kan inte läsa fil \\wsl$\ubuntu-20.04\home\project_directory\tsconfig.json

Detta visas på grund av mina parserOptions i min .eslintrc.json-fil.

Mina försök att lösa detta problem:

  • Jag följde denna tråd: Parsing error: Cannot read file '.../tsconfig.json'.eslint, genom att konvertera min json till en js-fil och använda __dirname. Men det löser inte problemet för mig.
  • Jag försökte också gå in i mitt IDE och manuellt välja eslint från mitt projekts node_modules. Ingenting verkar ta bort detta fel för mig.
  • Det konstiga är att om jag klonar mitt repo på Windows (inte WSL), och öppnar projektet på Windows, fungerar projektet bra och eslint visar inga problem. Vet inte varför det inte fungerar på samma sätt på WSL.

All hjälp skulle uppskattas eftersom jag är osäker på vad jag ska göra härnäst.

.eslintrc.json
{
    "env": {
        "browser": true,
        "es2021": true,
        "node": true,
        "jest": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
        "prettier"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 2021,
        "sourceType": "module",
        "project": "./tsconfig.json"
    },
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
        "@typescript-eslint/restrict-template-expressions": ["error", {
            "allowNumber": true,
            "allowBoolean": true,
            "allowAny": true,
            "allowNullable": true
        }],
        "no-use-before-define": [2, { "functions": false }],
        "no-underscore-dangle": 0,
        "block-scoped-var": 2,
        "no-undef": 2,
        "no-loop-func": 1,
        "no-console": 1,
        "no-debugger": 2,
        "eqeqeq": 2,
        "strict": [2, "function"],
        "no-unused-vars": [2, { "args": "none" }],
        "no-prototype-builtins": "warn"
    }
}

tsconfig.json
{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    /* Basic Options */
    "target": "ES6",                                /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
    "module": "ESNext",                           /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "sourceMap": true,                           /* Generates corresponding '.map' file. */
    "outDir": "dist",                              /* Redirect output structure to the directory. */
    "strict": true,                                 /* Enable all strict type-checking options. */
    "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                           /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true        /* Disallow inconsistently-cased references to the same file. */
  }
}

package.json
{
  "name": "backendbill",
  "version": "21.7.0",
  "description": "Running on AWS EC2 instance for api and bouncer",
  "main": "dist/index.js",
  "type": "module",
  "scripts": {
    "build": "rimraf dist && tsc",
    "test": "npx jest --watch",
    "dev": "cross-env NODE_ENV=development concurrently \"tsc --watch\" \"nodemon -q dist/index.js\"",
    "prod": "cross-env NODE_ENV=production concurrently \"tsc --watch\" \"nodemon -q dist/index.js\"",
    "deploy": "pm2 deploy ecosystem.config.cjs production",
    "lint": "eslint . --ext .ts",
    "stage": "echo \"No tests just yet\"",
    "prepare": "husky install",
    "preserve": "npm run build",
    "format": "prettier --write \"_/_.+(js|json)\""
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/..."
  },
  "author": "Brother Bill",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/..."
  },
  "homepage": "https://github.com/...",
  "dependencies": {
    "@types/body-parser": "^1.19.1",
    "@types/express": "^4.17.13",
    "@types/node": "^16.3.3",
    "body-parser": "^1.19.0",
    "cross-env": "^7.0.3",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "helmet": "^4.6.0",
    "rimraf": "^3.0.2",
    "typescript": "^4.3.5"
  },
  "devDependencies": {
    "@types/jest": "^26.0.24",
    "@typescript-eslint/eslint-plugin": "^4.28.3",
    "@typescript-eslint/parser": "^4.28.3",
    "concurrently": "^6.2.0",
    "eslint": "^7.31.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-promise": "^5.1.0",
    "husky": "^7.0.0",
    "lint-staged": "^11.0.1",
    "nodemon": "^2.0.12",
    "pm2": "^5.1.0",
    "prettier": "^2.3.2",
    "ts-jest": "^27.0.3"
  },
  "lint-staged": {
    "*.{js,ts,cjs}": [
      "eslint --cache --fix",
      "prettier --check"
    ]
  }
}

Konfigurationsskärmbilder:

Automatisk:

Manual:

Svar #1

Svaret, för de flesta som hittar denna sida, finns i kommentarssektionen:

Specifikt bör du se till att din node-version också använder wsl2-versionen av node (eller nvm), inte den hostinstallerade "Program Files"-versionen!

I mitt fall var det: /home/<me>/.nvm/versions/node/v18.19.1/bin/node

Relaterade artiklar