在 electron 中使用 sqlite3 模块时出错

在 electron 中使用 sqlite3 模块时出错

尝试在 Electron 中使用 NPM 包 sqlite3 时发生错误。我该怎么做才能让 electron-builder 运行?或者我该怎么做才能在 electron 中使用 sqlite3?

我的开发环境是

  • Windows 8.1 x64
  • 节点版本 v10.15.3
  • npm 版本 6.4.1。

我的 package.json 文件如下所示: 我的 package.json 文件

作为示例,以下项目文件:

index.js

const { app, BrowserWindow } = require('electron')
const sqlite3 = require("sqlite3")

function createWindow () {
  // Create the browser window.
  let win = new BrowserWindow({ width: 800, height: 600 })

  // and load the index.html of the app.
  win.loadFile('index.html')
}

app.on('ready', createWindow)

索引.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

在cmd终端输入命令后npm start,出现如下错误信息:

运行电子后出错

结果在Stack Overflow上找到了如下问题解决方案:
如何使用 sqlite3 模块和 electron?

npm run postinstall然后我对我的 package.json 文件进行了上述更改。在终端中执行命令后,出现以下错误消息

λ npm run postinstall

> [email protected] postinstall C:\Users\xxx\Desktop\xxx
> electron-builder install-app-deps

Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.
Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.
  • electron-builder version=20.39.0
  • rebuilding native production dependencies platform=win32 arch=x64
Error: C:\Program Files\nodejs\node.exe exited with code 1
Output:

> [email protected] install C:\Users\xxx\Desktop\xxx\node_modules\sqlite3
> node-pre-gyp install --fallback-to-build

Failed to execute 'C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=C:\Users\xxx\Desktop\xxx\node_modules\sqlite3\lib\binding\electron-v4.1-win32-x64\node_sqlite3.node --module_name=node_sqlite3 --module_path=C:\Users\xxx\Desktop\xxx\node_modules\sqlite3\lib\binding\electron-v4.1-win32-x64 --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=electron-v4.1' (1)

Error output:
node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp WARN Tried to download(403): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.0.6/electron-v4.1-win32-x64.tar.gz
node-pre-gyp WARN Pre-built binaries not found for [email protected] and [email protected] (electron-v4.1 ABI, unknown) (falling back to source compile with node-gyp)
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:509:16)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:153:21)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure" "--fallback-to-build" "--module=C:\\Users\\xxx\\Desktop\\xxx\\node_modules\\sqlite3\\lib\\binding\\electron-v4.1-win32-x64\\node_sqlite3.node" "--module_name=node_sqlite3" "--module_path=C:\\Users\\xxx\\Desktop\\xxx\\node_modules\\sqlite3\\lib\\binding\\electron-v4.1-win32-x64" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=electron-v4.1"
gyp ERR! cwd C:\Users\xxx\Desktop\xxx\node_modules\sqlite3
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute 'C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=C:\Users\xxx\Desktop\xxx\node_modules\sqlite3\lib\binding\electron-v4.1-win32-x64\node_sqlite3.node --module_name=node_sqlite3 --module_path=C:\Users\xxx\Desktop\xxx\node_modules\sqlite3\lib\binding\electron-v4.1-win32-x64 --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=electron-v4.1' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (C:\Users\xxx\Desktop\xxx\node_modules\node-pre-gyp\lib\util\compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:970:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
node-pre-gyp ERR! System Windows_NT 6.3.9600
node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\xxx\\Desktop\\xxx\\node_modules\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd C:\Users\xxx\Desktop\xxx\node_modules\sqlite3
node-pre-gyp ERR! node -v v10.15.3
node-pre-gyp ERR! node-pre-gyp -v v0.11.0
node-pre-gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\xxx\AppData\Roaming\npm-cache\_logs\2019-04-04T17_19_09_963Z-debug.log

    at ChildProcess.childProcess.once.code (C:\Users\xxx\Desktop\xxx\node_modules\builder-util\src\util.ts:244:14)
    at Object.onceWrapper (events.js:277:13)
    at ChildProcess.emit (events.js:189:13)
    at maybeClose (internal/child_process.js:970:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
From previous event:
    at _rebuild (C:\Users\xxx\Desktop\xxx\node_modules\app-builder-lib\src\util\yarn.ts:179:5)
    at rebuild (C:\Users\xxx\Desktop\xxx\node_modules\app-builder-lib\out\util\yarn.js:163:19)
    at C:\Users\xxx\Desktop\xxx\node_modules\app-builder-lib\src\util\yarn.ts:20:11
From previous event:
    at _installOrRebuild (C:\Users\xxx\Desktop\xxx\node_modules\app-builder-lib\out\util\yarn.js:71:28)
    at installOrRebuild (C:\Users\xxx\Desktop\xxx\node_modules\app-builder-lib\out\util\yarn.js:55:28)
    at C:\Users\xxx\Desktop\xxx\node_modules\electron-builder\src\cli\install-app-deps.ts:56:9
    at Generator.next (<anonymous>)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at _installAppDeps (C:\Users\xxx\Desktop\xxx\node_modules\electron-builder\out\cli\install-app-deps.js:176:26)
    at installAppDeps (C:\Users\xxx\Desktop\xxx\node_modules\electron-builder\out\cli\install-app-deps.js:144:26)
    at then (C:\Users\xxx\Desktop\xxx\node_modules\electron-builder\src\cli\cli.ts:46:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: `electron-builder install-app-deps`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\xxx\AppData\Roaming\npm-cache\_logs\2019-04-04T17_19_10_020Z-debug.log

答案1

不知道你是否已经修复了这个问题,但作为一种解决方法,我降级到了 electron 4.0.0,然后它找到了正确的 sqlite 二进制文件

相关内容