CI服务器执行ESLint命令失败:无法找到standard配置
问题描述
本地VSCode环境中ESLint配置运行完全正常,但在CI服务器执行lint命令 ./node_modules/.bin/eslint ./ --ext .ts 时失败,报错如下:
eslint:config-array-factory Config file found: /home/worker/workspace/-CI-CD-Pipeline-Node-Ts_DC-705/.eslintrc.json +0ms
eslint:config-array-factory Loading {extends:"standard"} relative to /home/worker/workspace/-CI-CD-Pipeline-Node-Ts_DC-705/.eslintrc.json +1ms
Oops! Something went wrong! :( ESLint: 6.6.0. ESLint couldn't find the config "standard" to extend from. Please check that the name of the config is correct. The config "standard" was referenced from the config file in "/home/worker/workspace/-CI-CD-Pipeline-Node-Ts_DC-705/.eslintrc.json".
解决思路与方案
我之前处理过类似的CI与本地环境不一致的问题,以下是几个大概率能解决问题的步骤:
1. 确保eslint-config-standard已添加到项目依赖
本地VSCode可能通过全局安装的包或者编辑器缓存自动找到了配置,但CI环境是全新的构建环境,必须把eslint-config-standard明确添加到项目的开发依赖中:
npm install eslint-config-standard --save-dev
安装完成后,记得提交更新后的package.json和package-lock.json(或yarn.lock)到代码仓库,这样CI在安装依赖时才能获取到这个包。
2. 检查CI的依赖安装命令
很多时候CI脚本会为了速度只安装生产依赖(比如执行npm install --production),但ESLint相关的配置都属于开发依赖,所以必须确保CI执行的是全量依赖安装:
# 替换成你CI中使用的命令 npm install # 如果你用yarn的话 yarn install
3. 验证ESLint与配置包的版本兼容性
你的ESLint版本是6.6.0,而不同版本的eslint-config-standard对ESLint版本有要求。比如eslint-config-standard@14.x系列是适配ESLint 6.x的,如果你安装的是更高版本的配置包,就会出现兼容性问题。可以指定兼容版本安装:
npm install eslint-config-standard@14 --save-dev
4. 核对.eslintrc.json的配置拼写
仔细检查你的.eslintrc.json中extends字段的拼写,确保是"standard"而不是类似"eslint-standard"或者其他错误名称:
{ "extends": "standard" }
5. 清除CI的依赖缓存
部分CI服务会缓存node_modules来加速构建,可能缓存里缺少了eslint-config-standard包。你可以在CI脚本中添加清除缓存的步骤,或者临时禁用node_modules缓存,测试是否能解决问题。
内容的提问来源于stack exchange,提问作者undefined

