我有一個專案,其中包含API(expess.js)和客戶端(Reactjs)。
Project/
├──客戶端/ //我所有的reactjs檔案。
├── src/ //我所有的expression.js檔案。
├──.eslintrc.json。
我正在嘗試配置eslint,對于Express.js部分我沒有任何問題。但對于react部分,我有以下錯誤:
/home/dev/project/client/index.js。
5:17 error Parsing error: Unexpected token <
下面是我的eslint配置
{
"env": {
"node": true。
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module".
},
"rules": {
"block-scoped-var": ["error"]。
"callback-return": ["錯誤", ["完成", "繼續", "下一步", "onwards", "callback", "cb"] ]。
"逗號式": ["警告", "最后"]。
"curly": ["警告"]。
"eqeqeq": ["錯誤", "總是"]。
"eol-last": ["警告"]。
"handle-callback-err": ["error"]。
"縮進": ["警告", 2, {
"SwitchCase": 1,
"MemberExpression": "off",
"FunctionDeclaration": {"body":1, "引數":"off"],
"FunctionExpression": {"body":1, "引數":"off"],
"CallExpression": {"arguments":"off"},
"ArrayExpression": 1,
"ObjectExpression": 1,
"ignoredNodes": ["ConditionalExpression"]。
}],
"lineebreak-style": ["error", "unix"]。
"no-dupe-keys": ["error"]。
"no-duplicate-case": ["error"]。
"no-extra-semi": ["警告"]。
"no-labels"。 ["error"]。
"no-mixed-spaces and-tabs": [2, "smart-tabs"] 。
"no-redeclare": ["警告"]。
"no-return-assign": ["error", "always"] 。
"no-sequences": ["error"]。
"no-trailing-spaces": ["警告"]。
"no-undef"。 ["off"]。
"no-unexpected-multiline": ["警告"]。
"no-unreachable": ["warning"]。
"no-unused-vars": ["warning", {"caughtErrors": "all", "catchErrorsIgnorePattern": "^unused($|[A-Z].*$)", "argsIgnorePattern": "^unused($|[A-Z].*$)", "varIgnorePattern": "^unused($|[A-Z].*$)" }] 。
"no-use-before-define": ["error", {"function":false}] 。
"one-var": ["警告", "從不"]。
"prefer-arrow-callback": ["warning", {"allowNamedFunctions":true}] 。
"quotes": ["warning", "single", {"voidEscape": false, "allowTemplateLiterals":true}]。
"semi": ["警告", "總是"]。
"半間距": ["警告", {"之前": false, "after":true}] 。
"semi-style": ["warning", "last"] 。
}
}
我試著添加了這幾行字:
我試著添加了這幾行字。
"overrides"/span>: [
{
"files": ["client/**/*.js"] 。
"決議器"。"@babel/eslint-parser"。
"parserOptions": {
"requireConfigFile": false,
"ecmaFeatures": {
"jsx": true。
}
},
"extends": [
"eslint:recommended",
"plugin:reaction/recommended".
],
"plugins": [
"reaction"/span>
]
}
],
但我仍然有一個錯誤
/home/dev/project/client/index.js。
5:16 error Parsing error: 這個實驗性語法需要啟用以下一個的分析器插件。'jsx, flow, typescript'(5:16)
在同一個專案中,如果要在Express.js后端和reactjs前端使用eslint,我應該怎么做?
uj5u.com熱心網友回復: 我終于找到了解決方案,在我使用的overrides行中,我把反應插件改為jsx。
為此我需要安裝一個新的包 這里是覆寫部分: 下面是完整的.eslintrc.json 我在這個文章中找到了解決方案
標籤:npm install eslint-plugin-jsx@latest -save-dev
"overrides"/span>: [
{
"files": ["client/**/*.js"] 。
"決議器"。"@babel/eslint-parser"。
"parserOptions": {
"requireConfigFile": false,
"ecmaFeatures": {
"jsx": true。
},
"babelOptions": {
"presets": ["@babel/preset-react"]
}
},
"extends": [
"eslint:commended",
"plugin:reaction/recommended".
],
"plugins": [
"jsx"/span>
]
}
],
{
"env"/span>: {
"node": true,
"es6": true。
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module".
},
"overrides": [
{
" files": ["client/**/*.js"] 。
"決議器"。"@babel/eslint-parser"。
"parserOptions": {
"requireConfigFile": false,
"ecmaFeatures": {
"jsx": true。
},
"babelOptions": {
"presets": ["@babel/preset-react"]
}
},
"extends": [
"eslint:commended",
"plugin:reaction/recommended".
],
"plugins": [
"jsx"/span>
]
}
],
"rules": {
"block-scoped-var": ["error"]。
"callback-return": ["錯誤", ["完成", "繼續", "下一步", "onwards", "callback", "cb"] ]。
"逗號式": ["警告", "最后"]。
"curly": ["警告"]。
"eqeqeq": ["錯誤", "總是"]。
"eol-last": ["警告"]。
"handle-callback-err": ["error"]。
"縮進": ["警告", 2, {
"SwitchCase": 1,
"MemberExpression": "off",
"FunctionDeclaration": {"body":1, "引數":"off"],
"FunctionExpression": {"body":1, "引數":"off"],
"CallExpression": {"arguments":"off"},
"ArrayExpression": 1,
"ObjectExpression": 1,
"ignoredNodes": ["ConditionalExpression"]。
}],
"lineebreak-style": ["error", "unix"]。
"no-dupe-keys": ["error"]。
"no-duplicate-case": ["error"]。
"no-extra-semi": ["警告"]。
"no-labels"。 ["error"]。
"no-mixed-spaces and-tabs": [2, "smart-tabs"] 。
"no-redeclare": ["警告"]。
"no-return-assign": ["error", "always"] 。
"no-sequences": ["error"]。
"no-trailing-spaces": ["警告"]。
"no-undef"。 ["off"]。
"no-unexpected-multiline": ["警告"]。
"no-unreachable": ["warning"]。
"no-unused-vars": ["warning", {"caughtErrors": "all", "catchErrorsIgnorePattern": "^unused($|[A-Z].*$)", "argsIgnorePattern": "^unused($|[A-Z].*$)", "varIgnorePattern": "^unused($|[A-Z].*$)" }] 。
"no-use-before-define": ["error", {"function":false}] 。
"one-var": ["警告", "從不"]。
"prefer-arrow-callback": ["warning", {"allowNamedFunctions":true}] 。
"quotes": ["warning", "single", {"voidEscape": false, "allowTemplateLiterals":true}]。
"semi": ["警告", "總是"]。
"半間距": ["警告", {"之前": false, "after":true}] 。
"semi-style": ["warning", "last"] 。
}
}
