简体   繁体   English

如何让eslint解析jsconfig中映射的路径

[英]How to make eslint resolve paths mapped in jsconfig

In my nextjs project I have mapped path in jsconfig.json to make easy absolute imports在我的 nextjs 项目中,我在jsconfig.json映射了路径,以便轻松进行绝对导入

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@/*": ["./*"]
    },
    "target": "es6",
    "module": "commonjs",
    "experimentalDecorators": true
  }
}

My import paths look like this import { VIEW } from '@/src/shared/constants';我的导入路径看起来像这样import { VIEW } from '@/src/shared/constants';

My eslintrc.js has settings specified as我的eslintrc.js设置指定为

module.exports = {
    ... ,
    settings: {
        "import/resolver": {
          alias: {
            extensions: [".js"],
            map: ["@", "."]
          }
        }
      }
}

I am still getting the error saying can't resolve "@/what/ever/my/path/is"我仍然收到错误消息,说无法解析“@/what/ever/my/path/is”

How do I make eslint realize the jsconfig path如何让eslint实现jsconfig路径

I was using babel-eslint as my parser in eslintrc.我在 eslintrc 中使用 babel-eslint 作为解析器。 While searching, I realized I need to add babel-plugin-module-resolver in babelrc to resolve the modules.在搜索时,我意识到我需要在 babelrc 中添加babel-plugin-module-resolver来解析模块。 In this file we can define our mapped paths which are there in our jsconfig.在这个文件中,我们可以定义我们的 jsconfig 中的映射路径。

Hence adding the following plugin in the babelrc file compiled my code successfully.因此在 babelrc 文件中添加以下插件成功编译了我的代码。

[
    "module-resolver",
    {
        "alias": {
          "@": "./"
        }
    }
]

According to the docs for eslint-import-resolver-alias , the map property should be an array of arrays, so try this:根据eslint-import-resolver-alias 的文档map属性应该是一个数组数组,所以试试这个:

module.exports = {
    ... ,
    settings: {
        "import/resolver": {
          alias: {
            extensions: [".js"],
            map: [ ["@", "."] ]
          }
        }
      }
}

Also, double-check that you actually have eslint-import-resolver-alias installed - it's easy to forget!另外,请仔细检查您是否确实安装了eslint-import-resolver-alias - 这很容易忘记!

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

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