vs工程打開一個js文件,會提示
"No ESLint configuration (e.g .eslintrc) found for file ......."
或
"Failed to load the ESLint library for the document ......."
就是需要配置eslint檢測環境
- 首先使用npm 全局安裝eslint
- 在項目目錄中執行eslint --init,這時候會讓你選擇項目的一些參數,比如模塊類型(JavaScript modules (import/export) CommonJS (require/exports) ),框架(vue或react),執行完畢后生成.eslintrc.js(報的錯不管)
- 如果選擇vue,還需要安裝eslint-plugin-vue插件,這時需要本地安裝這個插件
npm i -save-dev eslint-plugin-vue
- 需要對.eslintrc.js做一些配置,比如之前選擇了JavaScript modules,后面又使用module.exports,必須再加上
"commonjs": true
比如:
module.exports = {
"env": {
"browser": true,
"es6": true,
"commonjs": true
},
........
}
- 如果需要在控制台打印調試信息,需要加上:
"no-console":"off"
比如:
module.exports = {
......
"rules": {
"no-console":"off"
}
};
off也可以用0替代
"off" -> 0 關閉規則
"warn" -> 1 開啟警告規則
"error" -> 2 開啟錯誤規則
- 允許使用一些全局變量,比如node里的__dirname
"env": {
......
"node": true
},
- Eslint中no-undef的檢查報錯
有時候使用一些全局變量,會報這個錯誤,
這個可以在eslint中增加一個global配置,用於標記哪些可以使用的全局對象
"globals":{ "document": true, "localStorage": true, "window": true }