首页 > 解决方案 > sqlite3_1.default.database 不是构造函数

问题描述

sqlite3我通过纱线安装在我的 Electron-React-Typescript-Webpack 应用程序中:yarn add sqlite3@latest

并重建一切yarn electron-rebuild -f -w sqlite3

webpack.config.js我放:

const webpack = require('webpack');
    externals: [
      {
        'sqlite3': sqlite3,
      }
    ],

如果main.ts我输入:

import sqlite3 from 'sqlite3';

let db = new sqlite3.Database(
    path.join(app.getPath('userData'),'/infopiecesleveldb'),
    (err) => {
      if (err) {
        return console.error(err.message);
      }
      console.log('Connected to the in-memory SQlite database.');
});

我收到此错误:sqlite3_1.default.database is not a constructor

 (node:2679) UnhandledPromiseRejectionWarning: TypeError: sqlite3_1.default.database is not 
a constructor
    at App.<anonymous> (/home/marco/webMatters/electronMatters/Raphy-Template/dist/main 
/main.js:60:32)
    at App.emit (events.js:315:20)
(Use `electron --trace-warnings ...` to show where the warning was created)
(node:2679) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error 
originated either by throwing inside of an async function without a catch block, or by  
rejecting a promise which was not handled with .catch(). To terminate the node process on  
unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see  
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2679) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In 
the future, promise rejections that are not handled will terminate the Node.js process with   
a non-zero exit code.
 mainWindow loaded

关于如何解决它的任何想法?

标签: electronnode-sqlite3

解决方案


import sqlite3 from 'sqlite3';

//let db = new sqlite3.Database(':memory:', (err) => {
let db = new sqlite3.Database(
    path.join(app.getPath('userData'),'/infopiecessql3db'),
    (err) => {
      if (err) {
        return console.error(err.message);
      }
      console.log('Connected to the in-memory SQlite database.');
});
db.close();

现在我收到这条消息Connected to the in-memory SQlite database


推荐阅读