电子和续集错误:不支持方言sqlite

Jon*_*Jon 9 node.js sequelize.js electron

我正在尝试在桌面应用程序中使用sequelize和sqlite与电子,但在运行应用程序时运行npm start(运行node_modules/.bin/electron .)会出现以下错误:

未捕获的错误:不支持方言sqlite.(错误:请手动安装sqlite3包)

我已经安装了sequelize和sqlite npm install --save 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) }
Run Code Online (Sandbox Code Playgroud)

因此问题是使用电子续集.所有文件如下所示.

的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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;
    });
});
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

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
    }));
});
Run Code Online (Sandbox Code Playgroud)

使用安装依赖项npm install并使用重现问题npm start.跑步node models.js将显示其自身的续集作品.

ezp*_*zpn 7

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

我按照电子回购中提供的电子教程.

简单的方法

我安装了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
Run Code Online (Sandbox Code Playgroud)

节点-Gyp方式

我已经全局安装了node-gyp模块并输入了./node_modules/sqlite3dir.然后我尝试运行以下命令:

node-gyp rebuild --target=0.31.2 --arch=x64 --dist-url=https://atom.io/download/atom-shell
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

gyp: Undefined variable module_name in binding.gyp while trying to load binding.gyp
Run Code Online (Sandbox Code Playgroud)

npm Way

这导致了与The Easy Way相同的结果.

sqlite3分叉

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

最后的尝试 - 解决方案

@Josh提供的博客文章被我禁止,但我找到了它的谷歌缓存版本.我也跟着电子问题的讨论.

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

  • 在package.json中更改电子预建的版本 "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
    
    Run Code Online (Sandbox Code Playgroud)
  • 重建包

    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
    
    Run Code Online (Sandbox Code Playgroud)

我尝试使用电子预编译包的0.31.2版进行编译,但由于某种原因它失败了.

如果您使用的是Mac替换linuxdarwin.

如果你的操作系统架构是32位替换x64ia32


des*_*tan 7

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

当你放一个

console.log(err);

<project>/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' }
Run Code Online (Sandbox Code Playgroud)

但是,当您检查/node_modules/sqlite3/lib/binding/文件夹时,将没有node-v44-linux-x64文件夹,但文件夹之类的东西node-v11-linux-x64.(简单地重命名文件夹将不起作用.)

这种不匹配的发生是因为电子在io.js v3.1.0内部使用,因为它在这里说明它的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
Run Code Online (Sandbox Code Playgroud)

转到<project path>/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"
},
Run Code Online (Sandbox Code Playgroud)

然后给./node_modules/.bin/electron-rebuild命令稍等一下.然后它工作.