【问题标题】:Electron and sequelize error: the dialect sqlite is not supportedElectron 和 sequelize 错误:不支持方言 sqlite
【发布时间】:2015-12-02 02:10:03
【问题描述】:

我正在尝试在桌面应用程序中使用sequelize 和带有electron 的sqlite,但在通过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 将显示自己的续集作品。

【问题讨论】:

  • @destan 的答案比我的或 ezrepotein 好得多。恕我直言,您应该更改已接受的答案。

标签: node.js sequelize.js electron


【解决方案1】:

最后,我根据@Josh 提供的文章以及其他博客文章和问题讨论找到了解决此问题的有效方法。下面我写了我为解决这个问题而采取的所有步骤。 最终解决方案发布在此答案的底部

我按照 electron repo 提供的电子教程进行操作。

简单的方法

我已经安装了electron-rebuild 节点包并运行./node_modules/.bin/electron-rebuild,这给了我以下错误:

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 目录。然后我尝试运行以下命令:

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 相同的结果。

sqlite3 分叉

最后我尝试下载了一些sqlite3 forks。不幸的是,结果是一样的。

最终尝试 - 解决方案

@Josh 提供的博文对我来说是被禁止的,但我找到了它的google cached 版本。我也关注了electron issue的讨论。

下面介绍的步骤应该可以为您提供一个有效的 sqlite3 包。

  • 在你的 package.json 中更改 electron-prebuilt 的版本"electron-prebuilt": "0.29.1"
  • 重新安装electron-prebuilt
  • 将目录更改为./node_modules/sqlite3
  • 运行预发布脚本npm run prepublish
  • 配置 node-gyp module_name 和 module_path

    node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/node-v44-linux-x64
    
  • 重建包

    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 版本进行编译,但由于某种原因它失败了。

如果您使用的是 mac,请将 linux 替换为 darwin

如果您的操作系统架构是 32 位,请将 x64 替换为 ia32

【讨论】:

  • 当我运行node models.js 时它工作正常。 sqlite3 已安装在系统上。
  • 你使用的是哪个版本的 node.js?
  • 当我执行npm install sqlite --save 时,我在 NPM 中得到 404。你试过npm install sqlite3 --save吗?此外,您是否尝试过全局安装 SQLite3,因为 Electron 和常规 Node 环境可能不同。 npm install -g sqlite3 --save
  • @ezrepotein 我正在使用节点 v0.10.38。
  • @warsite sqlite 和 sqlite3 都适合我。我用 sqlite3 而不是 sqlite 尝试过,但我遇到了同样的问题。
【解决方案2】:

我知道您已安装 sqlite3 并单独工作,但是当您尝试将 sqlite3electron 一起使用时会出现问题。这是因为 ABI 版本不匹配。

当你放一个

console.log(err);

&lt;project&gt;/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js 第 21 行,

就在throw new Error('Please install sqlite3 package manually'); 之前,您会看到如下错误:

{ [Error: Cannot find module '<full_path_to_project>/node_modules/sqlite3/lib/binding/node-v44-linux-x64/node_sqlite3.node'] code: 'MODULE_NOT_FOUND' }

但是,当您检查 /node_modules/sqlite3/lib/binding/ 文件夹时,将没有 node-v44-linux-x64 文件夹,而是类似于 node-v11-linux-x64 文件夹。 (简单地重命名文件夹是行不通的。)

出现这种不匹配是因为 electron 在内部使用 io.js v3.1.0,因为它声明 here 和它的 ABI 版本,而您的 nodejs 版本不匹配。

请注意,node-vXX 由您节点的 ABI 版本决定。查看此网址了解更多信息:https://github.com/mapbox/node-pre-gyp/issues/167

解决方案

https://github.com/atom/electron/blob/master/docs/tutorial/using-native-node-modules.md#the-easy-way 此处所述的简单方法不适用于 sqlite,但您可以按照以下步骤使其工作:

通过以下命令安装electron-rebuild

npm install --save-dev electron-rebuild

转到&lt;project path&gt;/node_modules/sqlite3/node_modules/node-pre-gyp/lib/util/abi_crosswalk.js 并找到您的节点版本,然后将node_abi 值更改为44。如下:

"0.12.7": {
  "node_abi": 44,
  "v8": "3.28"
},

然后给出./node_modules/.bin/electron-rebuild 命令并稍等片刻。然后就可以了。

【讨论】:

  • 好吧,你已经写过“简单地重命名文件夹是行不通的”。但这是我的解决方案。
【解决方案3】:

如果您可以运行 $npm list sqlite3 并收到类似...的响应

MyAppName@0.0.1 /path/to/MyApp └── sqlite3@3.0.10

那么问题很可能是sqlite3在Electron中不起作用(没有一些强制)并且Sequelize不知道如何表达错误。

请参阅this blog post 以让 sqlite3 在 Electron 中工作。这应该可以解决您的 Sequelize 问题。

【讨论】:

【解决方案4】:

我多次遇到此错误,对我有用的解决方案是安装electron-rebuild,然后运行:

electron-rebuild -w sqlite3 -p

这里需要-p 标志才能工作,因为 sqlite3 使用 node-pre-gyp。

【讨论】:

  • App threw an error when running [Error: Please install sqlite3 package manually]
猜你喜欢
  • 2018-12-13
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
相关资源
最近更新 更多