Background
背景
Within an Electron app, sqlite3 doesn't work right after npm install --save
. I got it working after doing some web research, by:
在电子应用程序中,sqlite3在npm安装后不能立即运行——保存。我在做了一些网络研究之后,通过:
(1) downgrading my node version (via n
) to match Electron's node version; and
(1)降低我的节点版本(通过n)以匹配电子的节点版本;和
(2) rebuilding sqlite with the right module name/targets with this package.json
script: "rebuild-sqlite3": "cd node_modules/sqlite3 && npm run prepublish && node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-darwin-x64 && node-gyp rebuild --target=1.2.6 --arch=x64 --target_platform=darwin --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-darwin-x64"
(2)使用该包以正确的模块名/目标重新构建sqlite。json脚本:“rebuild-sqlite3”:“cd node_modules/sqlite3 & npm运行预发布和节点-gyp配置——module_name=node_sqlite3——module_path=../lib/binding/ electr- v1.2-darwin- xwin - xwin - npm & node-gyp configure———target= http:// http:// http:// http:// http://www.dars://dist - link—io /下载/ atom-shell——module_name = node_sqlite3 module_path = . . / lib /绑定/ electron-v1.2-darwin-x64”
After that, Sqlite3 works in development (i.e. when I run electron .
in the project dir).
之后,Sqlite3在开发中工作(例如,当我运行电子时。在项目的dir)。
Problem
问题
I package the app with electron-packager
: "./node_modules/.bin/webpack -p && ./node_modules/electron-packager/cli.js ./ --out ./bin --platform=darwin --arch=x64 --version=1.2.6 --overwrite --ignore=\"ignore|bin|node_modules\""
我用电子封装器将应用打包:“./node_modules/。”bin / webpack - p & & / node_modules / electron-packager / cli。/ -out ./bin -platform=darwin -arch=x64 -version=1.2.6 -overwrite -ignore=\“忽略|bin|node_modules”
When I open the app by double-clicking the created (appName).app, the console complains: `Uncaught Error: Cannot find module
当我通过双击创建的(appName)来打开应用程序时。app,控制台报错:“未捕获错误:找不到模块。
Question
问题
How can I get sqlite3 to be found in the final packaged product? Even "hacks" or workarounds are welcome, as I tried various fixes to no avail.
我怎样才能在最终的包装产品中找到sqlite3 ?即使是“黑客”或变通方法也很受欢迎,因为我尝试了各种各样的修复方法,但都无济于事。
2 个解决方案
#1
1
Doesn't the --ignore=\"ignore|bin|node_modules\"
you pass to electron-packager
ignore everything in the node_modules
dir, the very place where the sqlite3
module is installed?
难道——忽略=“忽略|bin|node_modules\”传递给电子包装器的忽略node_modules目录(sqlite3模块就是在这个目录中安装的)中的所有东西吗?
#2
1
I leave this answer for someone who has the same problem and is wasting their time to figure out.
我把这个答案留给一个有同样问题的人,而且是在浪费他们的时间去弄明白。
step1
step1
npm install sqlite3
npm安装sqlite3
electron .
电子。
I expected to work fine. but I had an error 'cannot find node module sqlite3'
我希望工作顺利。但我有个错误"找不到节点模块sqlite3 "
step2
步骤2
npm install electron-rebuild --save-dev
npm安装electron-rebuild——save-dev
electron-rebuild -f -w sqlite3
electron-rebuild - f - w sqlite3
electron .
电子。
it worked.
它工作。
but if you used 'electron-packager' to package and make executable file, It didn't work. the same error occured.
但是,如果您使用“电子包装器”来包装和制作可执行文件,它就不起作用。同样的错误发生。
"electron-packager . electron-tutorial-app --overwrite --asar --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=new --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Test\"",
step3
步骤3
I spent pretty much time to figure out. At the end, I figured out. the solution was using electron-packager API and integrating afterCopy with electron-rebuild.
我花了很多时间去弄清楚。最后,我明白了。解决方案是使用电子封装器API,将afterCopy与电子重建集成在一起。
# build.js
const packager = require('electron-packager');
const rebuild = require('electron-rebuild');
packager({
dir: '../dir',
overwrite: true,
asar: true,
platform: 'win32',
arch: 'ia32',
icon: 'assets/icons/win/icon.ico',
prune: true,
out: 'hello',
executableName: 'hello',
afterCopy: [(buildPath, electronVersion, platform, arch, callback) => {
rebuild.rebuild({ buildPath, electronVersion, arch })
.then(() => callback())
.catch((error) => callback(error));
}],
})
node build.js
节点build.js
reference
参考
-
electron-rebuild manual 'How can I integrate this into Electron packager?'
电子重建手册“我如何将它集成到电子包装器中?”
-
electron-packager API文档
#1
1
Doesn't the --ignore=\"ignore|bin|node_modules\"
you pass to electron-packager
ignore everything in the node_modules
dir, the very place where the sqlite3
module is installed?
难道——忽略=“忽略|bin|node_modules\”传递给电子包装器的忽略node_modules目录(sqlite3模块就是在这个目录中安装的)中的所有东西吗?
#2
1
I leave this answer for someone who has the same problem and is wasting their time to figure out.
我把这个答案留给一个有同样问题的人,而且是在浪费他们的时间去弄明白。
step1
step1
npm install sqlite3
npm安装sqlite3
electron .
电子。
I expected to work fine. but I had an error 'cannot find node module sqlite3'
我希望工作顺利。但我有个错误"找不到节点模块sqlite3 "
step2
步骤2
npm install electron-rebuild --save-dev
npm安装electron-rebuild——save-dev
electron-rebuild -f -w sqlite3
electron-rebuild - f - w sqlite3
electron .
电子。
it worked.
它工作。
but if you used 'electron-packager' to package and make executable file, It didn't work. the same error occured.
但是,如果您使用“电子包装器”来包装和制作可执行文件,它就不起作用。同样的错误发生。
"electron-packager . electron-tutorial-app --overwrite --asar --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=new --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Test\"",
step3
步骤3
I spent pretty much time to figure out. At the end, I figured out. the solution was using electron-packager API and integrating afterCopy with electron-rebuild.
我花了很多时间去弄清楚。最后,我明白了。解决方案是使用电子封装器API,将afterCopy与电子重建集成在一起。
# build.js
const packager = require('electron-packager');
const rebuild = require('electron-rebuild');
packager({
dir: '../dir',
overwrite: true,
asar: true,
platform: 'win32',
arch: 'ia32',
icon: 'assets/icons/win/icon.ico',
prune: true,
out: 'hello',
executableName: 'hello',
afterCopy: [(buildPath, electronVersion, platform, arch, callback) => {
rebuild.rebuild({ buildPath, electronVersion, arch })
.then(() => callback())
.catch((error) => callback(error));
}],
})
node build.js
节点build.js
reference
参考
-
electron-rebuild manual 'How can I integrate this into Electron packager?'
电子重建手册“我如何将它集成到电子包装器中?”
-
electron-packager API文档