当我在nextjs上使用babel别名时出现Eslint错误

问题描述 投票:0回答:1

我将module-resolver插件添加到.babelrc中,以便能够在我的nextjs项目中使用别名。

"plugins": [
    [
        "module-resolver",
        {
             "root": [
             "./"
             ],
             "alias": {
                "components": "./app/components",
                "pages": "./app/pages",
                "themes": "./app/themes"
             }
        }
    ]
]

一切都运作良好,但自从我添加了此配置后,我的eslint配置会生成导入/未解决的错误。

[eslint]无法解析模块'components / HOC'的路径。 [导入/无未解决]

为了解决这个问题,我尝试更新我的.eslintrc.json设置:

"settings": {
    "import/resolver": {
      "components": "./app/components",
      "pages": "./app/pages",
      "themes": "./app/themes"
    }  
},

但是,这种新配置无效。我想正确地解决这个问题,你有没有想法正确解决这个问题?

谢谢!

javascript reactjs babeljs eslint next.js
1个回答
1
投票

jsconfig.jsontsconfig.json for TypeScript)参考上添加此设置:https://github.com/tleunen/babel-plugin-module-resolver#editors-autocompletion

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "components": ["./app/components"],
      "pages": ["./app/pages"],
      "themes": ["./app/themes"]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.