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

升级Expo SDK 51后TypeORM DataSource无法正常工作求助

问题:Expo SDK 51升级后TypeORM连接expo-sqlite报错

升级Expo Go及Expo至SDK 51后,出现以下错误:

ERROR  [TypeError: _this3.sqlite.openDatabase is not a function. (In '_this3.sqlite.openDatabase(_this3.options.database)', '_this3.sqlite.openDatabase' is undefined)]

此前代码运行正常,相关代码及已尝试操作如下:

相关代码

database.ts

import { DataSource } from 'typeorm';
import 'reflect-metadata';
import User from '../Models/User';
import Message from '../Models/Message';
import Dialogue from '../Models/Chats/Dialogue';
import Tab from '../Models/Tab';
import SelfProfile from '../Models/SelfProfile';
import Folder from '../Models/Folder';
import Channel from '../Models/Chats/Channel';
import Branch from '../Models/Chats/Branch';
import Role from '../Models/Chats/Role';
import Group from '../Models/Chats/Group';
import Chat from '../Models/Chats/Chat';
import MainChat from '../Models/Chats/MainChat';

export const dataSource = new DataSource({
  database: 'messenger.db',
  type: 'expo',
  driver: require('expo-sqlite'),
  entities: [User, SelfProfile, Message, Tab, Folder, Chat, Branch,
    MainChat, Role, Dialogue, Channel, Group],
  synchronize: true,
  dropSchema: true,
  logging: ["error"],
});

Initialization.tsx

// some code before

if (!dataSource.isInitialized) {
  try {
    await dataSource.initialize();
  } catch(e) {
    console.error(e);
  }
}

// some code after

已尝试操作

  • 执行npx expo-doctor@latest检查项目状态
  • 执行npx expo install --fix修复依赖问题
    以上操作均未解决问题。

解决方案

1. 适配expo-sqlite的导出变化

Expo SDK 51中expo-sqlite的导出结构发生了改变,原require('expo-sqlite')无法直接获取到包含openDatabase的对象,需修改database.ts中的driver配置:

// 方案一:使用default导出
driver: require('expo-sqlite').default,

// 方案二:改用ES模块导入
import * as SQLite from 'expo-sqlite';

export const dataSource = new DataSource({
  // ...其他配置
  driver: SQLite,
});

2. 确保TypeORM版本兼容SDK 51

旧版TypeORM可能未适配Expo SDK 51的变化,建议升级到最新兼容版本:

npm install typeorm@latest

3. 清除缓存并重新安装依赖

# 删除依赖文件
rm -rf node_modules package-lock.json
# 重新安装指定依赖
npx expo install expo-sqlite typeorm reflect-metadata
# 清除Expo缓存并重启项目
npx expo start --clear

4. 验证实体模型配置

检查所有导入的实体模型(如User、Message等)是否正确使用了TypeORM的装饰器(如@Entity()),确保没有拼写错误或配置缺失。

内容的提问来源于stack exchange,提问作者Yevtushenko Hlib

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 15:43:33