Expo React Native应用--web模式运行时出现abort-controller错误求助
我运行命令 npx expo start --web 启动Expo Web应用时,控制台抛出以下错误:
ERROR in ./node_modules/abort-controller/polyfill.mjs:2
Module not found: Can't resolve './dist/abort-controller'
Did you mean 'abort-controller.mjs'?
BREAKING CHANGE: The request './dist/abort-controller' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '.mjs' file, or a '.js' file where the package.json contains "type": "module").
The extension in the request is mandatory for it to be fully specified.
以下是可行的解决方法:
更新abort-controller包到最新版本
这个问题大多是旧版本abort-controller的ESM模块路径解析bug,直接执行命令更新:npm update abort-controller # 用yarn的话执行: yarn upgrade abort-controller更新完成后重新运行
npx expo start --web,基本能解决问题。添加Webpack解析配置(更新包无效时用)
如果更新后还是报错,需要修改Expo的Webpack配置,让它自动补充模块扩展名:- 先安装依赖(没装过的话):
npm install --save-dev @expo/webpack-config - 在项目根目录创建
webpack.config.js,写入以下内容:const createExpoWebpackConfigAsync = require('@expo/webpack-config'); module.exports = async function (env, argv) { const config = await createExpoWebpackConfigAsync(env, argv); // 补充可解析的扩展名 config.resolve.extensions.push('.mjs', '.js', '.jsx', '.json'); // 针对abort-controller的路径别名(可选,仍报错时添加) config.resolve.alias['./dist/abort-controller'] = './dist/abort-controller.mjs'; return config; }; - 重新启动Web服务:
npx expo start --web
- 先安装依赖(没装过的话):
清除缓存重试
缓存异常也可能导致模块解析失败,执行命令清除缓存后重启:npx expo start --web --clear # 或者清除npm全局缓存: npm cache clean --force
内容的提问来源于stack exchange,提问作者knagode

