简体   繁体   English

使用 webpack 别名显示 eslint 错误

[英]eslint error showing with webpack alias

In my webpack config.在我的webpack配置中。 I defined aliases我定义了别名

alias: {
            components: 'src/components/',
            config: 'src/config/'
}

When I import a module from this path an eslint error occurred.当我从此路径导入模块时,发生了 eslint 错误。

import ShadowWrapper from 'components/ShadowWrapper'

error 'components' should be listed in the project's dependencies.错误“组件”应列在项目的依赖项中。 Run 'npm i -S components' to add it import/no-extraneous-dependencies运行'npm i -S components' 来添加它 import/no-extraneous-dependencies

Thanks, Pranav for the solution to this issue!谢谢,Pranav 解决了这个问题!
I add some code to this post to make it more practical for others.我在这篇文章中添加了一些代码,使其对其他人更实用。
First of all, in webpack config file I had defined this alias:首先,在 webpack 配置文件中我定义了这个别名:

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

That allow me to import components in my app in that way:这允许我以这种方式在我的应用程序中导入组件:

import NewsFeed from 'components/NewsFeed'

I have installed eslint-import-resolver-webpack plugin and put below code into .eslintrc.js or .eslintrc file :我已经安装了eslint-import-resolver-webpack插件并将以下代码放入 .eslintrc.js 或 .eslintrc 文件中:

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

That's it after running linter I got rid of Unable to resolve path to module 'components/NewsFeed' error message运行 linter 后就是这样,我摆脱了Unable to resolve path to module 'components/NewsFeed'错误消息的Unable to resolve path to module 'components/NewsFeed'
Hope it will be helpful for some of you!希望对你们中的一些人有所帮助!

This issue can be resolved by using the eslint-import-resolver-webpack这个问题可以通过使用eslint-import-resolver-webpack 解决

Here is what worked for me:这是对我有用的:

  1. I installed eslint-import-resolver-alias as dev dependency:我安装了eslint-import-resolver-alias作为开发依赖:

    npm install eslint-plugin-import eslint-import-resolver-alias --save-dev npm install eslint-plugin-import eslint-import-resolver-alias --save-dev

  2. In the Webpack config (in my case, it was Vue config, which is merged with Webpack config by Vue-cli), I added a few aliases:Webpack 配置中(在我的例子中,它是 Vue 配置,它通过 Vue-cli 与 Webpack 配置合并),我添加了一些别名:

     resolve: { extensions: ['.js', '.vue', '.json', '.less'], alias: { Site: path.resolve(__dirname, 'src/site'), Admin: path.resolve(__dirname, 'src/admin'), Common: path.resolve(__dirname, 'src/common'), Assets: path.resolve(__dirname, 'src/common/assets'), Configs: path.resolve(__dirname, 'src/common/configs'), Style: path.resolve(__dirname, 'src/common/style') } }
  3. In the .eslintsrc (or .eslintsrc.js, if you use that), I added the plugin and maps for these aliases, as follows:.eslintsrc (或 .eslintsrc.js,如果你使用它)中,我为这些别名添加了插件和映射,如下所示:

     "extends": ["plugin:import/recommended"], "settings": { "import/resolver": { "alias": { "map": [ ["Site", "./src/site"], ["Admin", "./src/admin"], ["Common", "./src/common"], ["Assets", "./src/common/assets"], ["Configs", "./src/common/configs"], ["Style", "./src/common/style"] ] }, "extensions": [".js", ".less", ".json", ".vue"] } }

I have added extensions for clarity and some good measures, which you can choose to not use for yourself.为了清晰起见,我添加了一些扩展和一些好的措施,您可以选择不使用它们。

Optional:可选的:

If you use VS Code and want these aliases working with the path intellisense, add a file jsconfig.json at the root, and specify your alias paths:如果您使用VS Code并希望这些别名与路径智能感知一起使用,在根目录添加一个文件jsconfig.json ,并指定您的别名路径:

{
    "compilerOptions": {
        "target": "esnext",
        "allowSyntheticDefaultImports": false,
        "baseUrl": "./",
        "paths": {
            "~/*": ["src/*"],
            "Root/*": ["src/*"],
            "Site/*": ["src/site/*"],
            "Admin/*": ["src/admin/*"],
            "Common/*": ["src/common/*"],
            "Assets/*": ["src/common/assets/*"],
            "Configs/*": ["src/common/configs/*"],
            "Style/*": ["src/common/style/*"]
        }
    },
    "exclude": ["node_modules", "dist"]
}

There are additional settings for React and Typescript. React 和 Typescript 有额外的设置。 Check the documentation at official site.检查官方网站上的文档

Another way, without mapping aliases between webpack.config.js and .eslintrc .另一种方式,没有在webpack.config.js.eslintrc之间映射别名。

You can use eslint-import-resolver-webpack and setup you .eslintrc file like this:您可以使用eslint-import-resolver-webpack并像这样设置.eslintrc文件:

{
  "extends": [
    "@hrplatform"
  ],
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"],
        "moduleDirectory": [
          "node_modules",
          "src"
        ]
      }
    }
  }
}

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

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