首页 > 解决方案 > 如何将绝对路径添加到 eslintrc?

问题描述

在我的 React 应用程序中,我试图向 SRC 添加绝对路径,以防止所有 ../..

我有以下 tsconfig :

  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "noImplicitAny": false,
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",
    "baseUrl": "src"
  },
  "include": [
    "src"
  ]
}

在一个组件中,我执行以下操作:

import { RootState } from 'app/services/store/rootReducer';
import { UsersActions } from 'app/services/store';

是编译和工作,但 eslint 说他没有找到一个模块。

Cannot find module 'app/services/store/rootReducer'

我试图将其添加到 eslintrc.json

  "plugins": [
    "react"
  ],
  "extends": [
    "eslint:recommended", 
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended"
  ],
  "parserOptions": {
    "ecmaVersion": 7,
    "sourceType": "module",
    "ecmaFeatures": {
        "jsx": true
    }
  },
  "env": {
    "browser": true,
    "node": true,
    "es6": true
  },
  "settings": {
    "import/resolver": {
      "node": {
        "paths": ["src"]
      },
      "es6": {
        "paths": ["src"]
      }
    }
  }
}``` 

But nothing changes.

How can I make absolute path work correctly ? 

标签: reactjseslint

解决方案


您应该像这样更改设置。之后,您必须安装“ eslint-plugin-import ”和“ eslint-import-resolver-babel-module

settings: {
    "import/resolver": {
      node: {},
      "babel-module": {
        root: ["./src"]
      }
    }
  }

推荐阅读