Create React App中Browserslist更新无效,caniuse-lite版本未变更求助
问题重现
基于Create React App搭建的项目,node_modules\caniuse-lite\package.json版本为1.0.30001251,执行yarn build时控制台提示:
Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
执行上述更新命令后,node_modules内的caniuse-lite版本未变化,yarn build仍重复提示更新,且项目的package.json与yarn.lock无任何改动。命令执行输出显示:
Latest version: 1.0.30001384 Installed version: none Removing old caniuse-lite from lock file Installing new caniuse-lite version $ yarn add -W caniuse-lite # (Warnings about peer dependencies...) Cleaning package.json dependencies from caniuse-lite $ yarn remove -W caniuse-lite # (Warnings about peer dependencies...) caniuse-lite has been successfully updated No target browser changes
问题原因
命令中使用了-W(workspace root)参数,导致更新操作针对的是工作区根目录而非当前项目,且更新完成后又自动移除了caniuse-lite,根本没作用到项目依赖树里的caniuse-lite(它是Create React App的子依赖)。
解决方法
方法1:用resolutions强制锁定版本
在项目的package.json中添加resolutions字段,强制所有子依赖使用指定版本的caniuse-lite:
"resolutions": { "caniuse-lite": "^1.0.30001384" }
添加后执行yarn install,这会强制把node_modules里的caniuse-lite更新到指定版本。
方法2:清理缓存后重新安装
- 清除yarn中caniuse-lite的缓存:
yarn cache clean caniuse-lite - 删除项目的
node_modules和yarn.lock:- macOS/Linux:
rm -rf node_modules && rm yarn.lock - Windows:
rmdir /s /q node_modules && del yarn.lock
- macOS/Linux:
- 重新安装依赖:
yarn install
方法3:调整更新命令参数
去掉-W参数,让命令在项目范围内执行更新:
npx browserslist@latest --update-db
或者明确指定yarn环境:
npx browserslist@latest --update-db --yarn
验证
执行完上述任一方法后,检查node_modules/caniuse-lite/package.json的版本是否已更新到1.0.30001384,再运行yarn build,应该不会再出现更新提示。
内容的提问来源于stack exchange,提问作者Marco Eckstein

