我正在尝试在桌面应用程序中使用sequelize和sqlite与electron但在通过 npm start
(运行 node_modules/.bin/electron .
)运行应用程序时出现以下错误:
未捕获错误:不支持方言sqlite . (错误:请手动安装sqlite3包)
我用 npm install --save sequelize sqlite
安装了sequelize和sqlite . 当我通过 node models.js
直接运行模型文件时,一切正常:
$ node models.js
Executing (default): CREATE TABLE IF NOT EXISTS `Users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(255), `birthday` DATETIME, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`Users`)
Executing (default): INSERT INTO `Users` (`id`,`username`,`birthday`,`updatedAt`,`createdAt`) VALUES (NULL,'janedoe','1980-07-19 22:00:00.000 +00:00','2015-09-06 11:18:52.412 +00:00','2015-09-06 11:18:52.412 +00:00');
{ id: 1,
username: 'janedoe',
birthday: Sun Jul 20 1980 00:00:00 GMT+0200 (CEST),
updatedAt: Sun Sep 06 2015 13:18:52 GMT+0200 (CEST),
createdAt: Sun Sep 06 2015 13:18:52 GMT+0200 (CEST) }
所以问题是使用电子续集 . 所有文件如下所示 .
package.json
{
"name": "example",
"version": "0.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node_modules/.bin/electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"devDependencies": {
"electron-prebuilt": "^0.31.1"
},
"dependencies": {
"jquery": "^2.1.4",
"sequelize": "^3.7.1",
"sqlite3": "^3.0.10"
}
}
app.js
var app = require('app');
var BrowserWindow = require('browser-window');
require('crash-reporter').start();
var mainWindow = null;
app.on('window-all-closed', function() {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('ready', function() {
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadUrl('file://' + __dirname + '/index.html');
mainWindow.on('closed', function() {
mainWindow = null;
});
});
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
</head>
<body>
<p>Example</p>
<script src="models.js"></script>
</body>
</html>
models.js
var Sequelize = require('sequelize');
var sequelize = new Sequelize('bdgt', 'username', 'password', {
dialect: 'sqlite',
storage: 'example.db',
});
var User = sequelize.define('User', {
username: Sequelize.STRING,
birthday: Sequelize.DATE
});
sequelize.sync().then(function() {
return User.create({
username: 'janedoe',
birthday: new Date(1980, 6, 20)
});
}).then(function(jane) {
console.log(jane.get({
plain: true
}));
});
使用 npm install
安装依赖项并使用 npm start
重现问题 . 运行 node models.js
将显示sequelize自己的作品 .
4 回答
我知道你已经安装了
sqlite3
并且单独工作但是当你尝试将sqlite3
与electron
一起使用时会出现问题 . 这是因为ABI版本不匹配 .当你放一个
console.log(err);
in<project>/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
第21行,就在
throw new Error('Please install sqlite3 package manually');
之前,你会看到如下错误:但是,当您检查
/node_modules/sqlite3/lib/binding/
文件夹时,将没有node-v44-linux-x64
文件夹,但类似于node-v11-linux-x64
文件夹 . (简单地重命名文件夹将不起作用 . )发生这种不匹配是因为电子在内部使用了
io.js v3.1.0
,因为它声明here并且它的ABI版本和你的nodejs版本不匹配 .请注意,
node-vXX
是通过节点的ABI版本决定的 . 查看此网址以获取更多信息:https://github.com/mapbox/node-pre-gyp/issues/167Solution
这里说明的简单方法https://github.com/atom/electron/blob/master/docs/tutorial/using-native-node-modules.md#the-easy-way与
sqlite
无法正常工作,但您可以按照以下步骤使其工作:通过以下命令安装
electron-rebuild
之后转到
<project path>/node_modules/sqlite3/node_modules/node-pre-gyp/lib/util/abi_crosswalk.js
并找到您的节点版本,然后将node_abi
值更改为44
. 如下:然后给
./node_modules/.bin/electron-rebuild
命令稍等一下 . 然后它工作 .我多次遇到此错误,对我有用的解决方案是安装
electron-rebuild
,然后运行:这里需要
-p
标志,因为sqlite3使用node-pre-gyp .如果你可以运行
$npm list sqlite3
并得到像...那样的回复MyAppName@0.0.1 /path/to/MyApp └── sqlite3@3.0.10
然后问题可能是sqlite3在Electron中不起作用(没有一些强制),而Sequelize不知道如何表达错误 .
请参阅this blog post以使sqlite3在Electron中工作 . 这应该用Sequelize解决你的问题 .
最后,根据@Josh提供的文章以及其他博客文章和问题讨论,我找到了解决此问题的有效方案 . 下面我写了我为解决这个问题而采取的所有步骤 . final solution 发布在 bottom of this answer
我按照electron repo中的电子教程 .
The Easy Way
我已经安装了
electron-rebuild
node package并运行./node_modules/.bin/electron-rebuild
,这给了我以下错误:The node-gyp Way
我已经全局安装了node-gyp模块并输入了
./node_modules/sqlite3
dir . 然后我尝试运行以下命令:并得到以下错误:
The npm Way
这导致与The Easy Way相同的结果 .
The sqlite3 forks
最后我试着下载了一些sqlite3 forks . 不幸的是结果是一样的 .
Final attempt - The solution
@Josh提供的博客文章是禁止的,但我找到了google cached版本 . 我也跟着electron issue的讨论 .
下面介绍的步骤应该为您提供一个有效的sqlite3包 .
更改package.json
"electron-prebuilt": "0.29.1"
中电子预建的版本重新安装
electron-prebuilt
将目录更改为
./node_modules/sqlite3
运行预发布脚本
npm run prepublish
配置node-gyp module_name和module_path
我尝试使用电子预构建版本的0.31.2版进行编译,但由于某种原因它失败了 .
If you are using mac replace linux with darwin.
If your os architecture is 32 bit replace x64 with ia32