我正在尝试使用eslint-import-resolver-babel-module,但没有成功。 Airbnb配置使用“节点”解析器,似乎只使用它。
这是我的配置:
{
"env": {
"browser": true
},
"extends": "airbnb",
"plugins": [
"react",
"jsx-a11y",
"import"
],
"rules": {
"no-param-reassign": [2, { "props": false }]
},
"parser": "babel-eslint",
"root": true,
"settings": {
"import/resolver": {
"babel-module": {}
}
}
}
我使用这个编译配置:
$ ./node_modules/.bin/eslint --print-config src/test.js > eslintconfig 2>&1
摘自结果:
...
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".jsx",
".json"
]
},
"babel-module": {}
},
...
然后我用这个命令测试
$ cross-env DEBUG=eslint-plugin-import:resolver:* ./node_modules/.bin/eslint src/test.js > eslintdebug 2>&1
输出
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node Resolving: client/test2 from: D:XXX\src\test.js
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node resolve threw error: { Error: Cannot find module 'client/test2' from 'D:XXX\src'
at Function.module.exports [as sync] (D:XXX\node_modules\resolve\lib\sync.js:40:15)
at Object.exports.resolve (D:XXX\node_modules\eslint-import-resolver-node\index.js:18:28)
at v2 (D:XXX\node_modules\eslint-module-utils\resolve.js:79:23)
at withResolver (D:XXX\node_modules\eslint-module-utils\resolve.js:84:16)
at fullResolve (D:XXX\node_modules\eslint-module-utils\resolve.js:101:22)
at relative (D:XXX\node_modules\eslint-module-utils\resolve.js:46:10)
at resolve (D:XXX\node_modules\eslint-module-utils\resolve.js:172:12)
at checkSourceValue (D:XXX\node_modules\eslint-plugin-import\lib\rules\no-unresolved.js:29:50)
at checkSourceValue (D:XXX\node_modules\eslint-module-utils\moduleVisitor.js:29:5)
at EventEmitter.checkSource (D:XXX\node_modules\eslint-module-utils\moduleVisitor.js:34:5) code: 'MODULE_NOT_FOUND' }
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node Resolving: client/test2 from: D:XXX\src\test.js
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node resolve threw error: { Error: Cannot find module 'client/test2' from 'D:XXX\src'
at Function.module.exports [as sync] (D:XXX\node_modules\resolve\lib\sync.js:40:15)
at Object.exports.resolve (D:XXX\node_modules\eslint-import-resolver-node\index.js:18:28)
at v2 (D:XXX\node_modules\eslint-module-utils\resolve.js:79:23)
at withResolver (D:XXX\node_modules\eslint-module-utils\resolve.js:84:16)
at fullResolve (D:XXX\node_modules\eslint-module-utils\resolve.js:101:22)
at relative (D:XXX\node_modules\eslint-module-utils\resolve.js:46:10)
at resolve (D:XXX\node_modules\eslint-module-utils\resolve.js:172:12)
at resolveImportType (D:XXX\node_modules\eslint-plugin-import\lib\core\importType.js:74:65)
at reportIfMissing (D:XXX\node_modules\eslint-plugin-import\lib\rules\no-extraneous-dependencies.js:74:32)
at EventEmitter.ImportDeclaration (D:XXX\node_modules\eslint-plugin-import\lib\rules\no-extraneous-dependencies.js:144:9) code: 'MODULE_NOT_FOUND' }
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node Resolving: client/test2 from: D:XXX\src\test.js
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node resolve threw error: { Error: Cannot find module 'client/test2' from 'D:XXX\src'
at Function.module.exports [as sync] (D:XXX\node_modules\resolve\lib\sync.js:40:15)
at Object.exports.resolve (D:XXX\node_modules\eslint-import-resolver-node\index.js:18:28)
at v2 (D:XXX\node_modules\eslint-module-utils\resolve.js:79:23)
at withResolver (D:XXX\node_modules\eslint-module-utils\resolve.js:84:16)
at fullResolve (D:XXX\node_modules\eslint-module-utils\resolve.js:101:22)
at relative (D:XXX\node_modules\eslint-module-utils\resolve.js:46:10)
at resolve (D:XXX\node_modules\eslint-module-utils\resolve.js:172:12)
at EventEmitter.ImportDeclaration (D:XXX\node_modules\eslint-plugin-import\lib\rules\no-duplicates.js:37:52)
at emitOne (events.js:101:20)
at EventEmitter.emit (events.js:191:7) code: 'MODULE_NOT_FOUND' }
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node Resolving: client/test2 from: D:XXX\src\test.js
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node resolve threw error: { Error: Cannot find module 'client/test2' from 'D:XXX\src'
at Function.module.exports [as sync] (D:XXX\node_modules\resolve\lib\sync.js:40:15)
at Object.exports.resolve (D:XXX\node_modules\eslint-import-resolver-node\index.js:18:28)
at v2 (D:XXX\node_modules\eslint-module-utils\resolve.js:79:23)
at withResolver (D:XXX\node_modules\eslint-module-utils\resolve.js:84:16)
at fullResolve (D:XXX\node_modules\eslint-module-utils\resolve.js:101:22)
at relative (D:XXX\node_modules\eslint-module-utils\resolve.js:46:10)
at resolve (D:XXX\node_modules\eslint-module-utils\resolve.js:172:12)
at EventEmitter.checkFileExtension (D:XXX\node_modules\eslint-plugin-import\lib\rules\extensions.js:81:50)
at emitOne (events.js:101:20)
at EventEmitter.emit (events.js:191:7) code: 'MODULE_NOT_FOUND' }
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node Resolving: client/test2 from: D:XXX\src\test.js
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node resolve threw error: { Error: Cannot find module 'client/test2' from 'D:XXX\src'
at Function.module.exports [as sync] (D:XXX\node_modules\resolve\lib\sync.js:40:15)
at Object.exports.resolve (D:XXX\node_modules\eslint-import-resolver-node\index.js:18:28)
at v2 (D:XXX\node_modules\eslint-module-utils\resolve.js:79:23)
at withResolver (D:XXX\node_modules\eslint-module-utils\resolve.js:84:16)
at fullResolve (D:XXX\node_modules\eslint-module-utils\resolve.js:101:22)
at relative (D:XXX\node_modules\eslint-module-utils\resolve.js:46:10)
at resolve (D:XXX\node_modules\eslint-module-utils\resolve.js:172:12)
at Function.ExportMap.get (D:XXX\node_modules\eslint-plugin-import\lib\ExportMap.js:281:38)
at checkDefault (D:XXX\node_modules\eslint-plugin-import\lib\rules\no-named-as-default.js:25:41)
at emitOne (events.js:101:20) code: 'MODULE_NOT_FOUND' }
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node Resolving: client/test2 from: D:XXX\src\test.js
Sun, 06 Aug 2017 14:16:33 GMT eslint-plugin-import:resolver:node resolve threw error: { Error: Cannot find module 'client/test2' from 'D:XXX\src'
at Function.module.exports [as sync] (D:XXX\node_modules\resolve\lib\sync.js:40:15)
at Object.exports.resolve (D:XXX\node_modules\eslint-import-resolver-node\index.js:18:28)
at v2 (D:XXX\node_modules\eslint-module-utils\resolve.js:79:23)
at withResolver (D:XXX\node_modules\eslint-module-utils\resolve.js:84:16)
at fullResolve (D:XXX\node_modules\eslint-module-utils\resolve.js:101:22)
at relative (D:XXX\node_modules\eslint-module-utils\resolve.js:46:10)
at resolve (D:XXX\node_modules\eslint-module-utils\resolve.js:172:12)
at Function.ExportMap.get (D:XXX\node_modules\eslint-plugin-import\lib\ExportMap.js:281:38)
at EventEmitter.handleImportDefault (D:XXX\node_modules\eslint-plugin-import\lib\rules\no-named-as-default-member.js:35:45)
at emitOne (events.js:101:20) code: 'MODULE_NOT_FOUND' }
[4mD:XXX\src\test.js[24m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-unresolved[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-extraneous-dependencies[22m
[2m1:1[22m [31merror[39m 'client' should be listed in the project's dependencies. Run 'npm i -S client' to add it [2mimport/no-extraneous-dependencies[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-duplicates[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/extensions[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-named-as-default[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-named-as-default-member[22m
[2m1:8[22m [31merror[39m 'test' is defined but never used [2mno-unused-vars[22m
[2m1:18[22m [31merror[39m Unable to resolve path to module 'client/test2' [2mimport/no-unresolved[22m
[2m1:18[22m [31merror[39m Missing file extension for "client/test2" [2mimport/extensions[22m
[31m[1m✖ 10 problems (10 errors, 0 warnings)
[22m[39m
在这里,您只能看到使用eslint-plugin-import:resolver:node
,但看不到eslint-plugin-import:resolver:babel-module
。
我也试过这个:
"settings": {
"import/resolver": "babel-module"
}
但结果更糟:
[4mD:XXX\src\test.js[24m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-unresolved[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-extraneous-dependencies[22m
[2m1:1[22m [31merror[39m 'client' should be listed in the project's dependencies. Run 'npm i -S client' to add it [2mimport/no-extraneous-dependencies[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-duplicates[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/extensions[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-named-as-default[22m
[2m1:1[22m [31merror[39m Resolve error: undefined [2mimport/no-named-as-default-member[22m
[2m1:8[22m [31merror[39m 'test' is defined but never used [2mno-unused-vars[22m
[2m1:18[22m [31merror[39m Unable to resolve path to module 'client/test2' [2mimport/no-unresolved[22m
[2m1:18[22m [31merror[39m Missing file extension for "client/test2" [2mimport/extensions[22m
[31m[1m✖ 10 problems (10 errors, 0 warnings)
[22m[39m
依赖版本:
$ npm ls babel-plugin-module-resolver eslint-import-resolver-babel-module eslint-config-airbnb eslint-plugin-import eslint
house@0.1.0 D:\XXX
+-- babel-plugin-module-resolver@2.7.1
+-- eslint@3.19.0
+-- eslint-config-airbnb@15.0.2
+-- eslint-import-resolver-babel-module@3.0.0
`-- eslint-plugin-import@2.7.0
如何覆盖父配置的插件设置?
答案 0 :(得分:1)
结果eslint-plugin-import
获取了一个解析器列表,并逐个检查它们,直到其中一个解析器正确解析文件。