【发布时间】:2021-01-08 20:31:24
【问题描述】:
对于一个小测试项目,我尝试使用 sqlite db 和 typeorm 启动 NativeScript(Angular 风格)。
ns create sample-typeorm --ng
cd sample-typeorm
ns plugin add nativescript-sqlite
npm i typeorm stream-browserify timers-browserify
由于缺少 nodejs 模块“流”和“计时器”,我的 tsconfig.json 看起来像
{
"compilerOptions": {
"module": "esnext",
"target": "es2017",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers": true,
"noEmitOnError": true,
"skipLibCheck": true,
"lib": [
"es2018",
"es2017",
"dom",
"es6"
],
"baseUrl": ".",
"paths": {
"~/*": [
"app/*"
],
"timers": [
"node_modules/timers-browserify"
],
"stream": [
"node_modules/stream-browserify"
]
}
},
"include": [
"src/tests/**/*.ts",
"src/**/*.ios.ts",
"src/**/*.android.ts"
],
"files": [
"./references.d.ts",
"./src/main.ts"
],
"exclude": [
"node_modules",
"platforms",
"e2e"
]
}
为了开始typoeorm/sqlite 测试,我在src/main.ts 中添加了“createConnection”内容:
// this import should be first in order to load some required settings (like globals and reflect-metadata)
import {platformNativeScriptDynamic} from "@nativescript/angular";
import {AppModule} from "./app/app.module";
import {createConnection} from "typeorm/browser";
let driver = require('nativescript-sqlite');
platformNativeScriptDynamic().bootstrapModule(AppModule);
(async () => {
try {
const connection = await createConnection({
database: 'test.db',
type: 'nativescript',
entities: [
//... whatever entities you have
],
driver,
logging: true,
synchronize: false
})
} catch (e) {
console.error(e)
}
;
})();
我启动应用程序
ns run ios --emulator
在构建系统工作一段时间后,出现以下错误:
ERROR in ../node_modules/app-root-path/lib/resolve.js
Module not found: Error: Can't resolve 'module' in '/private/tmp/sample-typeorm/node_modules/app-root-path/lib'
@ ../node_modules/app-root-path/lib/resolve.js 7:18-35
@ ../node_modules/app-root-path/lib/app-root-path.js
@ ../node_modules/app-root-path/index.js
@ ../node_modules/typeorm/browser/connection/ConnectionOptionsReader.js
@ ../node_modules/typeorm/browser/index.js
@ ./main.ts
在node_modules/app-root-path/lib/resolve.js中是错误的来源:
var globalPaths = require('module').globalPaths;
“模块”模块是否有等效的浏览器?或者 webpack 有没有一个选项可以避免这个错误?
为了更进一步,我修改了resolve.js 以跳过“模块”并遇到下一个错误,这让我想到我还有另一个丢失/未配置的 webpack 选项:
Terminating app due to uncaught exception 'NativeScript encountered a fatal error: Uncaught TypeError: Cannot destructure property 'env' of 'global.process' as it is undefined.
at
../node_modules/supports-color/index.js(file: node_modules/supports-color/index.js:6:7)
在node_modules/supports-color/index.js是这样的:
const {env} = process;
webpack.config.js 没有受到“ns create ...”的影响。
有没有人运行 NativeScript 7 的 typeorm 示例?或者知道缺少哪个模块?
【问题讨论】:
-
我被困在同一个地方,但是我没有添加流和计时器的 broswerify 版本。相反,我使用了以下 ns nodeify 插件、natievscript-xml2js lib、@nativescript-community/typeorm lib。我还尝试了
npm i module,这导致了您的最后一个错误。 Typeorm 是一个很棒的库,希望有人能阐明如何解决。
标签: webpack nativescript typeorm nativescript-angular