Пакет узла sqlite3 не работает на простом Angular 9 приложении - PullRequest
0 голосов
/ 06 марта 2020

Я создал простое приложение angular9, используя ng new electron-angular-demo3' npm install npm install --save @types/node npm install sqlite3 --build-from-source упакованный скомпилированный файл 'lib / binding / node-v72-win32-x64 / node_sqlite3.node' и 'build \ Release \ node_sqlite3. lib '' build \ Release \ sqlite3.lib '

enter image description here, затем я написал следующий код в моем app.component.ts

import { Component } from '@angular/core';
import * as sqlite3 from 'sqlite3';
//declare var require: any

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'electron-angular-demo3';
  constructor() {
    const sqlite3 = require('sqlite3').verbose();
    //let db = new sqlite3.Database(':memory:');
  }
}

tsconfig. json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ],
    //"types": ["node"],
    "typeRoots": [ 
      "../node_modules/@types"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

в пакете tsconfig.app. json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    /*"types": ["node"],
    "typeRoots": [ 
      "../node_modules/@types"
    ]*/
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

. json

{
  "name": "electron-angular-demo3",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.5",
    "@angular/common": "~9.0.5",
    "@angular/compiler": "~9.0.5",
    "@angular/core": "~9.0.5",
    "@angular/forms": "~9.0.5",
    "@angular/platform-browser": "~9.0.5",
    "@angular/platform-browser-dynamic": "~9.0.5",
    "@angular/router": "~9.0.5",
    "rxjs": "~6.5.4",
    "sqlite3": "^4.1.1",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.5",
    "@angular/cli": "~9.0.5",
    "@angular/compiler-cli": "~9.0.5",
    "@angular/language-service": "~9.0.5",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.12.29",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "~3.7.5"
  }
}

ng build получил следующее ошибки

ERROR in src/app/app.component.ts:14:21 - error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i @types/node`.

14     const sqlite3 = require('sqlite3').verbose();
                       ~~~~~~~

Если я удаляю параметр "типы" комментариев в tsconfig. json, я получаю много ошибок компиляции

Я думал, что проблема была в моем большом электронном приложении, поэтому Я создал это простое приложение, используя angular. Может кто-нибудь помочь, пожалуйста? enter image description here

...