You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Expo在安卓设备打开React Native应用报错无法解析os模块

问题根因

报错是因为你安装的uniqid依赖Node.js原生核心模块os,React Native移动端运行环境没有内置该模块,浏览器端正常是因为web打包工具默认对Node核心模块做了polyfill适配。


解决方案

方案1:替换为React Native兼容的唯一ID生成库(最推荐)

直接停用依赖Node核心模块的uniqid,换用专门适配React Native环境的ID生成库,操作步骤:

  • 卸载uniqid:
npm uninstall uniqid
# 用yarn的话执行:yarn remove uniqid
  • 安装react-native-uuid作为替代:
npm install react-native-uuid
# 用yarn的话执行:yarn add react-native-uuid
  • 替换代码里的引用逻辑即可,用法示例:
import uuid from 'react-native-uuid';
// 生成唯一ID,替代之前的uniqid()调用
const id = uuid.v4();

方案2:给os模块添加polyfill(需要保留uniqid时使用)

如果业务逻辑必须使用uniqid,可以手动配置polyfill适配React Native环境:

  • 安装os的浏览器适配版本:
npm install os-browserify
# 用yarn的话执行:yarn add os-browserify
  • 项目根目录新建metro.config.js文件,添加如下配置:
const { getDefaultConfig } = require('@expo/metro-config');

const config = getDefaultConfig(__dirname);
config.resolver.alias = {
  ...config.resolver.alias,
  'os': 'os-browserify'
};

module.exports = config;
  • 清除Metro缓存重启项目:
npx expo start --reset-cache

操作完成后重新编译安卓端即可恢复正常。

内容的提问来源于stack exchange,提问作者abdo sayed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:06:02