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

Ionic5+Angular12项目TypeORM报错:无法解析react-native-sqlite-storage

解决Ionic5+Angular12中TypeORM 0.3.20找不到'react-native-sqlite-storage'的问题

你的项目基于Ionic 5与Angular 12,使用TypeORM 0.3.20时出现以下错误:

"Module not found: Error: Can't resolve 'react-native-sqlite-storage' in '/node_modules/typeorm/browser/driver/react-native'"

项目环境:Node v16.20.0,TypeScript 4.2.3,package.json配置如下:

{
  "name": "test",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "lint": "ng lint",
    "postinstall": "ngcc"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^12.2.17",
    "@angular/common": "^12.2.17",
    "@angular/compiler": "^12.2.17",
    "@angular/core": "^12.2.17",
    "@angular/forms": "^12.2.17",
    "@angular/platform-browser": "^12.2.17",
    "@angular/platform-browser-dynamic": "^12.2.17",
    "@angular/router": "^12.2.17",
    "@ionic-native/core": "5.36.0",
    "@ionic-native/network": "5.36.0",
    "@ionic-native/splash-screen": "5.36.0",
    "@ionic-native/sqlite": "^5.36.0",
    "@ionic-native/status-bar": "5.36.0",
    "@ionic/angular": "^5.9.4",
    "@ionic/storage": "2.1.3",
    "@ngui/auto-complete": "^3.0.0",
    "@okta/okta-auth-js": "7.5.1",
    "@types/lodash": "4.14.116",
    "angular-oauth2-oidc": "12.1.0",
    "angular2piwik": "0.1.5-beta",
    "bootstrap": "5.3.3",
    "classlist.js": "^1.1.20150312",
    "cordova-android": "^11.0.0",
    "cordova-plugin-device": "^2.0.3",
    "cordova-plugin-statusbar": "^2.4.3",
    "cordova-sqlite-storage": "^6.0.0",
    "core-js": "^3.36.0",
    "flickity": "2.1.2",
    "font-awesome": "4.7.0",
    "intl": "^1.2.5",
    "ionic-plugin-keyboard": "^2.2.1",
    "ionicons": "^5.0.0",
    "jquery": "3.2.1",
    "karma": "^6.0.0",
    "karma-chrome-launcher": "^3.2.0",
    "karma-coverage-istanbul-reporter": "^3.0.3",
    "karma-jasmine": "^5.1.0",
    "karma-jasmine-html-reporter": "^1.7.0",
    "karma-junit-reporter": "^2.0.1",
    "moment": "2.22.2",
    "popper.js": "1.12.6",
    "postcss-px2rem-exclude": "^0.0.6",
    "puppeteer": "^5.2.1",
    "reflect-metadata": "^0.2.1",
    "rxjs": "~6.5.5",
    "sqlite3": "^5.1.7",
    "sw-toolbox": "3.6.0",
    "tslib": "^2.3.0",
    "typeorm": "^0.3.20",
    "web-animations-js": "^2.2.5",
    "zone.js": "0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^12.0.0",
    "@angular-eslint/builder": "^12.0.0",
    "@angular-eslint/eslint-plugin": "^12.0.0",
    "@angular-eslint/eslint-plugin-template": "^12.0.0",
    "@angular-eslint/schematics": "^12.0.0",
    "@angular-eslint/template-parser": "^12.0.0",
    "@angular/cli": "^12.0.0",
    "@angular/compiler-cli": "^12.0.0",
    "@angular/language-service": "^12.0.0",
    "@ionic/angular-toolkit": "^4.0.0",
    "@types/jasmine": "~5.1.0",
    "@types/node": "^14.14.31",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "babel-plugin-react-native-web": "^0.19.10",
    "cordova-plugin-androidx-adapter": "^1.1.3",
    "cordova-plugin-inappbrowser": "^5.0.0",
    "cordova-plugin-ionic-webview": "^5.0.0",
    "cordova-plugin-network-information": "^3.0.0",
    "eslint": "^7.26.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "typescript": "~4.2.3"
  },
  "description": "An Ionic project"
}

解决方案

1. 指定TypeORM使用Cordova驱动

TypeORM 0.3.x默认会尝试加载React Native驱动,但你的项目是Cordova环境,需在配置中明确指定Cordova驱动:

import { DataSource } from "typeorm";

export const AppDataSource = new DataSource({
    type: "cordova",
    database: "your-db-name.db",
    driver: require("typeorm/browser/driver/cordova/CordovaDriver"),
    entities: [/* 你的实体类路径 */],
    synchronize: true, // 根据业务需求调整
});

2. 配置Angular打包时忽略React Native模块

在angular.json的项目构建配置中,添加路径映射,让打包工具跳过查找react-native-sqlite-storage:
找到projects -> test -> architect -> build -> options,添加以下配置:

"options": {
  // 原有配置...
  "baseUrl": "./",
  "paths": {
    "react-native-sqlite-storage": ["node_modules/typeorm/browser/driver/cordova/CordovaDriver.js"]
  }
}

3. 降级TypeORM版本(推荐)

TypeORM 0.3.x对Angular 12的兼容性较差,降级到0.2.x版本(如0.2.45)能更好适配Ionic5+Angular12环境:

npm uninstall typeorm
npm install typeorm@0.2.45

降级后只需在配置中设置type: "cordova"即可,无需额外指定驱动路径。

4. 确认Cordova SQLite插件安装状态

虽然package.json已声明依赖,仍需确认插件已添加到Cordova项目:

cordova plugin list

若未找到cordova-sqlite-storage,重新安装:

cordova plugin add cordova-sqlite-storage@6.0.0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 20:09:52