webpack路径别名引发ESLint报错Unable to resolve path to module ‘***‘

解决方法:就是使用 eslint-import-resolver-webpack 这个插件。

场景复原,例如平时我们在 webpack 中设置了这样的一个别名路径:

alias:{
  components: path.resolve(__dirname, "src", "components")
}

然后我们使用别名去加载模块路径:

import NewsFeed from 'components/NewsFeed'

但是在执行 ESLint 校验之后,会报错:Unable to resolve path to module ‘components/NewsFeed’。

问题解决:eslint-import-resolver-webpack 插件的使用

首先安装依赖:

npm install eslint-import-resolver-webpack --save-dev

然后在你的 ESLint 配置文件中添加以下配置内容:

settings: {
  'import/resolver': {
    alias: {
      map: [
        ['components', './src/components']
      ]
  }
}

配置完毕,重新启动你的项目,问题解决。

猜你喜欢

转载自blog.csdn.net/weixin_40599109/article/details/107848176
今日推荐