刚开始接触electron ,踩到了electron-prebuilt的坑,简单记录下。
1、直接安装
npm install sqlite3 electron .
报错:'cannot find node module sqlite3'
2、重新编译sqlite3
2.1
npm install electron-rebuild --save-dev
npm install sqlite3 --runtime=electron --target=9.0.3 --dist-url=https://atom.io/download/electron
electron-rebuild -f -w sqlite3
node_modules/.bin/electron.cmd.
或者:packge.json的script下面添加
"rebuild": "electron-rebuild -f -w sqlite3", "postinstall": "electron-builder install-app-deps"
执行对应的命令
npm run rebuild
报错:
App threw an error during load Error: Cannot find module 'E:* ode_modulessqlite3libindingelectron-v1.4-win32-x64 ode_sqlite3.node' at Module._resolveFilename (module.js:455:15) at Function.Module._resolveFilename (E:* ode_moduleselectron-prebuiltdist esourceselectron.asarcommon eset-search-paths.js:35:12) at Function.Module._load (module.js:403:25) at Module.require (module.js:483:17) at require (internal/module.js:20:19) at Object.<anonymous> (E:* ode_modulessqlite3libsqlite3-binding.js:4:15) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12)
2.2上面已经编译成功,但是找不到electron-v1.4-win32-x64 ,同一目录已经生成electron-v9.0-win32-x64,尝试拷贝重命名electron-v1.4-win32-x64
App threw an error during load Error: A dynamic link library (DLL) initialization routine failed. \?E:* ode_modulessqlite3libindingelectron-v1.4-win32-x64 ode_sqlite3.node at Error (native) at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20) at Object.Module._extensions..node (module.js:583:18) at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.require (module.js:483:17) at Object.<anonymous> (E:* ode_modulessqlite3libsqlite3-binding.js:4:15)
仍然报错,库版本不对
3、为何electron-v1.4-win32-x64?
node_modules/.bin/electron.cmd -v 出来的
居然是V1.4.13
什么情况,我的electron版本是9.0.3啊
原来 electron-rebuild之后node_modules/.bin/electron.cmd版本居然会变
@ECHO off SETLOCAL CALL :find_dp0 IF EXIST "%dp0% ode.exe" ( SET "_prog=%dp0% ode.exe" ) ELSE ( SET "_prog=node" SET PATHEXT=%PATHEXT:;.JS;=;% ) "%_prog%" "%dp0%..electron-prebuiltcli.js" %* ENDLOCAL EXIT /b %errorlevel% :find_dp0 SET dp0=%~dp0 EXIT /b
居然指向了electron-prebuilt,版本也是electron-prebuilt的。
可能是一个bug吧。手动改回
"%_prog%" "%dp0%..electroncli.js" %*
可以使用如下代码测试下:
var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database(':memory:'); // 这段代码用来测试 SQLite db.serialize(function() { db.run("CREATE TABLE lorem (info TEXT)"); var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); for (var i = 0; i < 10; i++) { stmt.run("Ipsum " + i); } stmt.finalize(); db.each("SELECT rowid AS id, info FROM lorem", function(err, row) { console.log(row.id + ": " + row.info); }); }); db.close();
此时一切正常。
good luck
参考链接:
https://stackoverflow.com/questions/38600940/packaged-electron-app-cannot-find-module-sqlite3
https://stackoverflow.com/questions/38716594/electron-app-cant-find-sqlite3-module
https://juejin.im/entry/5b77cbf8f265da4325153f31