javascript 如何更改 eslint 设置以了解绝对导入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50234858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change eslint settings to understand absolute import?
提问by Alexander Knyazev
I use create-react-app and I want to use absolute import from ./src.
我使用 create-react-app 并且我想使用从./src.
As Dan Abramov recommendedI added .envwith NODE_PATH=srcand it works.
正如丹·阿布拉莫夫 (Dan Abramov)推荐的那样,我添加.env了NODE_PATH=src它并且它有效。
But my eslint doesn't understand that module already exists. I get error import/no-unresolvedand import/extensions
但是我的 eslint 不明白该模块已经存在。我得到错误import/no-unresolved和import/extensions
This is my eslint-config:
这是我的 eslint 配置:
module.exports = {
parser: 'babel-eslint',
extends: 'airbnb',
rules: {
'react/no-did-mount-set-state': 'off',
'import/no-extraneous-dependencies': 'off',
'no-use-before-define': 'off',
'arrow-body-style': 'off',
// uncommit on developing
'no-console': 'off',
'no-debugger': 'off',
},
globals: {
browser: true,
fetch: true,
serviceworker: true,
describe: true,
it: true,
expect: true,
document: true,
},
};
};
And of course it will good if vscode will make suggests for me by 'src'.
当然,如果 vscode 会通过 'src' 为我提出建议,那当然很好。
回答by Guillaume Jasmin
You need to use eslint-plugin-import: https://github.com/benmosher/eslint-plugin-import
您需要使用eslint-plugin-import:https: //github.com/benmosher/eslint-plugin-import
And configure your eslint settings in .eslintrc
并在中配置您的 eslint 设置 .eslintrc
module.exports = {
...
"settings": {
"import/resolver": {
"node": {
"paths": ["src"]
}
},
},
}
Then, you can use import from srcfolder.
然后,您可以使用从src文件夹导入。
Example, if you have src/components/MyComponent.jsx, you will write this:
例如,如果你有src/components/MyComponent.jsx,你会这样写:
import MyComponent from 'components/MyComponent.jsx';
回答by Omar Bahareth
You're extending airbnb's eslint config, which includes eslint-plugin-import, which has the following rule enabled by default: no-absolute-path.
您正在扩展 airbnb 的 eslint 配置,其中包括eslint-plugin-import,默认情况下启用以下规则:no-absolute-path.
See: https://github.com/benmosher/eslint-plugin-import/blob/HEAD/docs/rules/no-absolute-path.md
请参阅:https: //github.com/benmosher/eslint-plugin-import/blob/HEAD/docs/rules/no-absolute-path.md
You may need to tweak more rules beyond that.
除此之外,您可能需要调整更多规则。

