简体   繁体   English

eslint导入webpack:无法解析模块路径

[英]eslint imports in webpack: Unable to resolve path to module

With eslint-import-resolver-webpack I'm trying to let eslint know that I want to import files, by referencing a "shortcut" (eg. components/Counter). 使用eslint-import-resolver-webpack,我试图通过引用“快捷方式”(例如,components / Counter)让eslint知道我要导入文件。

In the example below, webpack (v2.2.0-rc.3) builds fine, but the test fails because it's "Unable to resolve path to module 'components/Counter'". 在下面的示例中,webpack(v2.2.0-rc.3)可以很好地构建,但是测试失败,因为它是“无法解析模块'组件/计数器'的路径”。 I get this error also for importing 'components/Counter/reducer', so it's not limited to index. 导入“组件/计数器/归约器”时也会出现此错误,因此它不仅限于索引。

Both running npm run lint and my IDE (IntelliJ) give the same error. 运行npm run lint和我的IDE(IntelliJ)都给出相同的错误。

Following the docs, I've set the following configs: 在文档之后,我设置了以下配置:

(Part of) file structure: (部分)文件结构:

project-root/
  app/
    components/
      Counter/
        index.js
        reducer.js
    app.js
 internals/ 
   config/
      .eslintrc
      webpack.test.js
  package.json

File: project-root/app/app.js 文件:project-root / app / app.js

import Counter from 'components/Counter'
// etc...

Excerpt of: project-root/package.json 摘录:project-root / package.json

...
"lint": "npm run lint:eslint -- . ",
"lint:eslint": "eslint -c internals/config/.eslintrc --fix --ignore-path .gitignore --ignore-pattern internals"
...

File: project-root/config/.eslintrc 文件:project-root / config / .eslintrc

{
  "parser": "babel-eslint",
  "extends": "airbnb",
  "env": {
    "browser": true,
    "node": true,
    "mocha": true,
    "es6": true
  },
  "plugins": [
    "redux-saga",
    "react",
    "jsx-a11y"
  ],
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "settings": {
    "import/resolver": {
      "webpack": {
        "config": "./internals/config/webpack.test.js"
      }
    }
  },
  "rules": {
    "arrow-parens": [
      "error",
      "always"
    ],
    "import/imports-first": 0,
    "import/newline-after-import": 0,
    "import/no-dynamic-require": 0,
    "import/no-extraneous-dependencies": 0,
    "import/no-named-as-default": 0,
    "import/prefer-default-export": 0,
    "jsx-a11y/aria-props": 2,
    "jsx-a11y/heading-has-content": 0,
    "jsx-a11y/href-no-hash": 2,
    "jsx-a11y/label-has-for": 2,
    "jsx-a11y/mouse-events-have-key-events": 2,
    "jsx-a11y/role-has-required-aria-props": 2,
    "jsx-a11y/role-supports-aria-props": 2,
    "max-len": [
      2,
      120,
      2,
      {
        "ignoreComments": true,
        "ignoreTrailingComments": false,
        "ignoreRegExpLiterals": true
      }
    ],
    "newline-per-chained-call": [
      2,
      {
        "ignoreChainWithDepth": 4
      }
    ],
    "no-console": [
      1,
      {
        "allow": [
          "warn",
          "error"
        ]
      }
    ],
    "no-use-before-define": [
      2,
      {
        "functions": false,
        "classes": true
      }
    ],
    "prefer-template": 2,
    "class-methods-use-this": 0,
    "react/forbid-prop-types": 0,
    "react/jsx-uses-vars": 2,
    "react/jsx-first-prop-new-line": [
      2,
      "multiline"
    ],
    "react/jsx-filename-extension": 0,
    "react/jsx-no-target-blank": 0,
    "react/prefer-stateless-function": 0,
    "react/require-extension": 0,
    "react/self-closing-comp": 0,
    "redux-saga/no-yield-in-race": 2,
    "redux-saga/yield-effects": 2,
    "semi": [
      2,
      "never"
    ]
  }
}

And: project-root/internals/config/webpack.test.js: 并且:project-root / internals / config / webpack.test.js:

const webpack = require('webpack')

module.exports = {
  devtool: 'inline-source-map',

  module: {
    loaders: [
      { test: /\.json$/, loader: 'json-loader' },

      { test: /\.css$/, loader: 'null-loader' },

      { test: /\.js$/,
        loader: 'babel',
        exclude: [/node_modules/],
      },

      { test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
        loader: 'null-loader',
      },
    ],
  },

  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV),
      },
    }),
  ],

  resolve: {
    modules: [
      'app',
      'node_modules',
    ],
  },
}

Note: I've also tried pointing to a file that only exports the resolve object. 注意:我也尝试过指向仅导出resolve对象的文件。 Same thing: 一样:

module.exports = {
  resolve: {
    modules: [
      'app',
      'node_modules',
    ],
  }
}

It seemed my dependencies needed updating... 看来我的依赖项需要更新...

There's quite a lot of plugins, configs, etc needed for a thorough linting process.. 彻底的起毛过程需要大量的插件,配置等。

-- edit -- -编辑-

Here's an excerpt of my projects' dependencies. 这是我项目的依赖关系的摘录。 There might be some packages in here that are not necessary, I removed only the ones of which I'm sure that don't have an impact. 这里可能有一些不必要的软件包,我只删除了那些肯定不会造成影响的软件包。

Note that front-end land moves like a cheetah in 6th gear, so the versions might get outdated soon. 请注意,前端区域在6档时像猎豹一样移动,因此版本可能很快就会过时。 Anyway, as of now (feb 2017) this is a working configuration. 无论如何,截至目前(2017年2月),这是一个有效的配置。

"dependencies": {
    "babel-polyfill": "6.16.0",
    "react": "15.4.2",
    "react-dom": "15.4.2",
  },
  "devDependencies": {
    "babel-cli": "6.18.0",
    "babel-core": "6.21.0",
    "babel-eslint": "7.1.0",
    "babel-loader": "6.2.10",
    "babel-plugin-react-transform": "2.0.2",
    "babel-preset-es2015": "6.18.0",
    "babel-preset-react": "6.16.0",
    "babel-preset-stage-3": "6.17.0",
    "babel-register": "6.22.0",
    "eslint": "3.13.1",
    "eslint-config-airbnb": "14.0.0",
    "eslint-import-resolver-webpack": "0.8.0",
    "eslint-plugin-import": "2.2.0",
    "eslint-plugin-jsx-a11y": "3.0.2",
    "eslint-plugin-react": "6.9.0",
    "eslint-plugin-redux-saga": "0.1.5",
    "webpack": "2.2.0",
    "webpack-dev-middleware": "1.9.0",
    "webpack-hot-middleware": "2.14.0"
  }

暂无
暂无

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

相关问题 eslint:无法解析模块“src/interfaces”的路径 - eslint: Unable to resolve path to module 'src/interfaces' tsconfig 中的 eslint + typescript + 路径:ESLint:无法解析模块“@components/some-module”的路径。(import/no-unresolved) - eslint + typescript + path in tsconfig: ESLint: Unable to resolve path to module '@components/some-module'.(import/no-unresolved) Next.js + eslint:无法解析模块“fs/promises”的路径 - Next.js + eslint: unable to resolve path to module 'fs/promises' 无法解析模块“react-native-screens”(ESLint)的路径 - Unable to resolve path to module 'react-native-screens' (ESLint) 指向文件而不是目录时,Eslint 无法解析模块的路径 - Eslint's Unable to resolve path to module when pointing to file and not a directory Eslint + typescript 路径别名 - 无法解析模块的路径 - Eslint + typescript paths aliasing - Unable to resolve path to module [eslint]'react-redux'应该在项目的依赖项中列出,无法解析模块'react-redux'的路径 - [eslint]'react-redux' should be listed in the project's dependency+unable to resolve path to module 'react-redux' 如何解决道具和同名模块导入之间的eslint冲突? - How do I resolve eslint conflict between props and module imports with the same name? 无法解析模块“@nivo/pie”的路径 - Unable to resolve path to module '@nivo/pie' 是否可以在 Webpack 编译时解析导入的模块路径? - Is it possible to resolve imported module path at compile time in Webpack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM