electron - 在 Electron 中打开 sqlite3 数据库
问题描述
我在 Electron-React-Typescript-Webpack 应用程序中导入 sql3 没有问题:
import sqlite3 from 'sqlite3';
但是当我在 main.ts 添加这些行时:
const sql3_cb = () => {
console.log("Welcome to sql3");
}
app.on('ready', async () => {
createMainWindow();
const infopiecesSqliteDb = new sqlite3.database(
path.join(app.getPath('userData'), '/infopiecessql3db'),
sql3_cb,
);
}
运行应用程序时出现以下错误(完整回溯):
(base) marco@pc01:~/webMatters/electronMatters/Raphy-Template$ yarn
start
yarn run v1.22.5
$ yarn run build && electron ./dist/main/main.js
$ npx webpack --config ./webpack.config.js
asset main.js 4.95 MiB [compared for emit] (name: main)
orphan modules 263 KiB [orphan] 138 modules
runtime modules 1.06 KiB 6 modules
javascript modules 3.6 MiB
cacheable modules 3.59 MiB
modules by path ./node_modules/ 3.58 MiB 660 modules
modules by path ./src/ 13.3 KiB 3 modules
json modules 374 KiB
modules by path ./node_modules/har-schema/lib/*.json 6.93 KiB 18
modules
modules by path ./node_modules/iconv-lite/encodings/tables/*.json
86.7 KiB 8 modules
modules by path ./node_modules/node-pre-gyp/ 16.8 KiB 3 modules
modules by path ./node_modules/ajv/lib/refs/*.json 5.58 KiB 3
modules
4 modules
WARNING in ./node_modules/node-pre-gyp/lib/pre-binding.js 20:22-48
Critical dependency: the request of a dependency is an expression
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 42:0-44
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
WARNING in ./node_modules/node-pre-gyp/lib/util/compile.js 39:27-49
Module not found: Error: Can't resolve 'npm' in '/home/marco
/webMatters/electronMatters/Raphy-Template/node_modules/node-pre-
gyp/lib/util'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/compile.js
./util/compile
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
WARNING in ./node_modules/node-pre-gyp/lib/util/versioning.js 17:20-67
Critical dependency: the request of a dependency is an expression
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/versioning.js
./util/versioning
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
WARNING in ./node_modules/sqlite3/lib/sqlite3-binding.js 4:14-35
Critical dependency: the request of a dependency is an expression
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
4 warnings have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show
it.
ERROR in ./node_modules/node-gyp/lib/Find-VisualStudio.cs 9:6
Module parse failed: Unexpected token (9:6)
You may need an appropriate loader to handle this file type, currently
no loaders are configured to process this file. See
https://webpack.js.org/concepts#loaders
| // This script needs to be compatible with PowerShell v2 to run on
Windows 2008R2 and Windows 7.
|
> using System;
| using System.Text;
| using System.Runtime.InteropServices;
@ ./node_modules/node-gyp/lib/ sync ^\.\/.*$ ./Find-VisualStudio.cs
@ ./node_modules/node-gyp/lib/node-gyp.js 41:13-36 190:36-53
@ ./node_modules/node-pre-gyp/lib/util/compile.js 29:28-55
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/compile.js
./util/compile
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
ERROR in ./node_modules/node-pre-gyp/lib/info.js 14:14-32
Module not found: Error: Can't resolve 'aws-sdk' in '/home/marco
/webMatters/electronMatters/Raphy-Template/node_modules/node-pre-
gyp/lib'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./info.js ./info
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
ERROR in ./node_modules/node-pre-gyp/lib/publish.js 18:14-32
Module not found: Error: Can't resolve 'aws-sdk' in '/home/marco
/webMatters/electronMatters/Raphy-Template/node_modules/node-pre-
gyp/lib'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./publish.js
./publish
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
ERROR in ./node_modules/node-pre-gyp/lib/unpublish.js 16:14-32
Module not found: Error: Can't resolve 'aws-sdk' in '/home/marco
/webMatters/electronMatters/Raphy-Template/node_modules/node-pre-
gyp/lib'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./unpublish.js
./unpublish
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
ERROR in ./node_modules/node-pre-gyp/lib/util/nw-pre-gyp/index.html
1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently
no loaders are configured to process this file. See
https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/nw-pre-
gyp/index.html
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/main/main.ts 35:16-34
3 errors have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show
it.
webpack 5.31.2 compiled with 5 errors and 4 warnings in 12546 ms
assets by path lib/pdfjs-dist/web/ 4.55 MiB 341 assets
assets by path assets/ 1.4 MiB
assets by path assets/css/ 1.23 MiB 22 assets
assets by path assets/pics/ 170 KiB 9 assets
assets by path *.js 11.5 MiB 7 assets
assets by path lib/pdfjs-dist/build/ 6.3 MiB
assets by path lib/pdfjs-dist/build/*.map 4 MiB 3 assets
assets by path lib/pdfjs-dist/build/*.js 2.3 MiB 3 assets
assets by path *.html 28 KiB 5 assets
asset lib/pdfjs-dist/LICENSE 9.94 KiB [compared for emit] [from:
src/lib/pdfjs-dist/LICENSE] [copied]
asset lib/infopiecemgr.js 2.77 KiB [compared for emit] [from: src/lib
/infopiecemgr.js] [copied]
orphan modules 35.2 KiB [orphan] 118 modules
runtime modules 7.32 KiB 32 modules
modules by path ./node_modules/ 3.38 MiB 850 modules
modules by path ./src/ 132 KiB
modules by path ./src/app/ 55.9 KiB 16 modules
modules by path ./src/app_A/ 24.8 KiB 4 modules
modules by path ./src/app_B/ 24.8 KiB 4 modules
modules by path ./src/store/*.ts 3.5 KiB
./src/store/redux_hooks.ts 556 bytes [built] [code generated]
./src/store/store.ts 2.96 KiB [built] [code generated]
modules by path ./src/assets/css/*.css 21 KiB
./src/assets/css/global.css 339 bytes [built] [code generated]
./node_modules/css-loader/dist/cjs.js!./src/assets/css/global.css
20.7 KiB [built] [code generated]
./src/reducers/infopiece/infopieceSlice.ts 1.98 KiB [built] [code
generated]
webpack 5.31.2 compiled successfully in 12552 ms
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about
this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about
this command.
看起来错误与 node-pre-gyp 和/或 webpack 配置有关。但我不明白如何解决它们。如何在电子应用程序中使用 sqlite3?
(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 pro
解决方案
推荐阅读
- javascript - Zapier 代码:将字符串转换为数组 javascript
- sql - 使用 SQL 为帐户设置生成唯一的访问密钥
- xamarin - 我们如何将 Grids Width 绑定到 xamarin 表单中通用视图模型中的属性列表项?
- java - 如何将 a.jar 转换为另一个自定义扩展?
- authentication - 如何在 Keycloak 中限制 SAML 客户端的某些组的用户身份验证?
- delphi - 线程可以安全地创建 FMX.Graphics.TBitmap.Canvas 吗?
- python - 如何使用 Python3 中的对数属性确保一个数字是另一个数字的幂?
- sql-server - 如何根据具有不同 where 条件和按包含撇号的字段名称分组的日期表最大值获取结果?
- svn - .svnignore 在执行时被 Subversion 忽略:“svn copy”
- javascript - 延迟阅读php代码