Electron 和 sequelize 错误:不支持方言 sqlite [英] Electron and sequelize error: the dialect sqlite is not supported
问题描述
我正在尝试将 sequelize 和 sqlite 与 electron 在桌面应用程序中,但在通过 npm start
运行应用程序时出现以下错误(运行 node_modules/.bin/electron .
):
I'm trying to use sequelize and sqlite with electron in a desktop application but get the following error when running the app via npm start
(which runs node_modules/.bin/electron .
):
未捕获的错误:不支持方言 sqlite.(错误:请手动安装 sqlite3 包)
Uncaught Error: The dialect sqlite is not supported. (Error: Please install sqlite3 package manually)
我已经使用 npm install --save sequelize sqlite
安装了 sequelize 和 sqlite.当我直接通过 node models.js
运行模型文件时,一切正常:
I've installed sequelize and sqlite with npm install --save sequelize sqlite
. When I run the models file directly via node models.js
, everything works fine:
$ 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) }
所以问题是特定于使用电子续集.所有文件如下所示.
So the problem is specific to using sequelize with electron. All files are shown below.
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 自己的作品.
Install the dependencies using npm install
and reproduce the problem using npm start
. Running node models.js
will show sequelize works one its own.
推荐答案
根据@Josh 提供的文章以及其他博客文章和问题讨论,我终于找到了解决这个问题的方法.下面我写了我为解决这个问题而采取的所有步骤.最终解决方案发布在此答案的底部
Finally I found a working solution to this problem, based on article provided by @Josh, and other blog posts and issue discussions. Below I wrote all steps I have taken in order to solve this problem. The final solution is posted on the bottom of this answer
我遵循 电子回购.
简单的方法
我已经安装了 electron-rebuild
节点包并运行 ./node_modules/.bin/electron-rebuild
这给了我以下错误:
I've installed electron-rebuild
node package and run ./node_modules/.bin/electron-rebuild
which gave me the following error:
node-pre-gyp ERR! install error
node-pre-gyp ERR! stack Error: Unsupported target version: 0.31.2
node-pre-gyp ERR! command "node" "/my/project/dir/node_modules/sqlite3/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! not ok
npm ERR! Failed at the sqlite3@3.0.10 install script 'node-pre-gyp install --fallback-to-build'.
npm ERR! This is most likely a problem with the sqlite3 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-pre-gyp install --fallback-to-build
node-gyp 方式
我已经全局安装了 node-gyp 模块并进入了 ./node_modules/sqlite3
目录.然后我尝试运行以下命令:
I've installed node-gyp module globally and entered ./node_modules/sqlite3
dir. Then I tried to run the following command:
node-gyp rebuild --target=0.31.2 --arch=x64 --dist-url=https://atom.io/download/atom-shell
并得到以下错误:
gyp: Undefined variable module_name in binding.gyp while trying to load binding.gyp
npm 方式
这导致了与 The Easy Way 相同的结果.
This resulted in the same results as The Easy Way.
sqlite3 分叉
最后我尝试下载一些 sqlite3 forks.不幸的是,结果是一样的.
Finally I tried to download few of the sqlite3 forks. Unfortunately results were the same.
最终尝试 - 解决方案
@Josh 提供的博文对我来说是被禁止的,但我发现 google 缓存的版本.我也关注了电子问题的讨论.
The blog post provided by @Josh was forbidden for me, but I found google cached version of it. I also followed the discussion of the electron issue.
下面介绍的步骤应该可以为您提供一个有效的 sqlite3 包.
Steps presented below should get you a working sqlite3 package.
- 在你的 package.json 中更改 electron-prebuilt 的版本
"electron-prebuilt": "0.29.1"
- 重新安装
electron-prebuilt
- 将目录更改为
./node_modules/sqlite3
- 运行预发布脚本
npm run prepublish
配置 node-gyp module_name 和 module_path
- Change version of electron-prebuilt in your package.json
"electron-prebuilt": "0.29.1"
- Reinstall
electron-prebuilt
- Change directory into
./node_modules/sqlite3
- Run prepublish scripts
npm run prepublish
Configure node-gyp module_name and module_path
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/node-v44-linux-x64
重建包
Rebuild package
node-gyp rebuild --target=0.29.1 --arch=x64 --target_platform=linux --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/node-v44-linux-x64
我尝试使用 electron-prebuilt 包的 0.31.2 版本进行编译,但由于某种原因它失败了.
I tried to compile using version 0.31.2 of electron-prebuilt package, but it failed for some reason.
如果您使用的是 mac,请将 linux
替换为 darwin
.
If you are using mac replace linux
with darwin
.
如果您的操作系统架构是 32 位,请将 x64
替换为 ia32
If your os architecture is 32 bit replace x64
with ia32
这篇关于Electron 和 sequelize 错误:不支持方言 sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!