dva應用git提交時提示"Cannot find module 'eslint-config-defaults/configurations/eslint'"的解決辦法
阿新 • • 發佈:2019-02-14
問題背景
dva應用程式碼git提交時,vscode提示"Cannot find module 'eslint-config-defaults/configurations/eslint ' Referenced from: c:\Users\xxx\.eslintrc
原因分析
使用eslintrc語法檢查模組的dva應用在git提交時,vscode會從專案的component目錄及其父級目錄(英文ancestors)尋找本地的.eslintrc檔案(eslint的配置檔案)。
如果找不到該檔案,則會尋找C:/Users/xxx/.eslintrc檔案(這個是全域性配置檔案)。
我們開啟一個這樣的全域性檔案
{ /* See all the pre-defined configs here: https://www.npmjs.com/package/eslint-config-defaults */ "extends": "defaults/configurations/eslint", "parser": "babel-eslint", "ecmaFeatures": { "jsx": true }, "plugins": [ "react" ], "env": { "amd": true, "browser": true, "jquery": true, "node": true, "es6": true, "worker": true }, "rules": { "eqeqeq": 2, "comma-dangle": 1, "no-console": 0, "no-debugger": 1, "no-extra-semi": 1, "no-extra-parens": 1, "no-irregular-whitespace": 0, "no-undef": 0, "no-unused-vars": 0, "semi": 1, "semi-spacing": 1, "valid-jsdoc": [ 2, { "requireReturn": false } ], "react/display-name": 2, "react/forbid-prop-types": 1, "react/jsx-boolean-value": 1, "react/jsx-closing-bracket-location": 1, "react/jsx-curly-spacing": 1, "react/jsx-indent-props": 1, "react/jsx-max-props-per-line": 0, "react/jsx-no-duplicate-props": 1, "react/jsx-no-literals": 0, "react/jsx-no-undef": 1, "react/jsx-sort-prop-types": 1, "react/jsx-sort-props": 0, "react/jsx-uses-react": 1, "react/jsx-uses-vars": 1, "react/no-danger": 1, "react/no-did-mount-set-state": 1, "react/no-did-update-set-state": 1, "react/no-direct-mutation-state": 1, "react/no-multi-comp": 1, "react/no-set-state": 0, "react/no-unknown-property": 1, "react/prop-types":0, "react/react-in-jsx-scope": 0, "react/require-extension": 1, "react/self-closing-comp": 1, "react/sort-comp": 1, "react/wrap-multilines": 1 } }
可以看到,檔案引用了defaults/configurations/eslint;報上述錯誤一般是因為專案並沒有安裝這個配置檔案裡引用的模組。
解決辦法
辦法1、
仍然使用eslint,安裝'eslint-config-defaults/configurations/eslint'模組;
npm指令
npm install --save-dev eslint eslint-config-defaults
一般會在git提交的時候提示很多語法規範上的錯誤,這些規則都是eslint配置檔案定義的;照著改就行
辦法2、
刪除全域性配置檔案,在專案根目錄下新增本地.eslintrc檔案;可以將該檔案內容自定義修改為自己需要的內容;如下面是我自己的版本;只需要umi;沒有引用報錯的模組,同時也捨去了繁瑣的語法檢查(當然一般不建議這麼做,語法檢查有助於保證多人協作開發過程中的程式碼風格統一,便於維護);
{
"extends": "umi"
}