简体   繁体   English

电子应用程序找不到 sqlite3 模块

[英]Electron app cant find sqlite3 module

In my electron app I have installed sqlite3 via npm在我的电子应用程序中,我通过 npm 安装了 sqlite3

npm install sqlite3

But once i try to interact with the database it cant find the database, here is the log:但是一旦我尝试与数据库交互,它就找不到数据库,这是日志:

Uncaught Error: Cannot find module 'D:\\play\\electron-quick-start\\node_modules\\sqlite3\\lib\\binding\\electron-v1.3-win32-x64\\node_sqlite3.node'未捕获的错误:找不到模块 'D:\\play\\electron-quick-start\\node_modules\\sqlite3\\lib\\binding\\electron-v1.3-win32-x64\\node_sqlite3.node'

Here is JS code:这是JS代码:

console.log('whooooo');

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('../db/info.db');

db.serialize(function () {
    db.run("CREATE TABLE lorem (info TEXT)");   

    var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
    for (var i = 0; i < 10; i++) {
        stmt.run("Ipsum " + i);
    }
    stmt.finalize();

    db.each("SELECT rowid AS id, info FROM lorem", function (err, row) {
        console.log(row.id + ": " + row.info);
    });
});
db.close();

I also try in this way:我也是这样尝试的:

npm install sqlite3 --build-from-source

but it fails to install!但是安装失败!

Also, i am using Python3.另外,我正在使用 Python3。 How do you install a module to work with electron?你如何安装一个模块来与电子一起工作?

Firstly:首先:

npm install electron-rebuild

then try this several times:然后尝试几次:

./node_modules/.bin/electron-rebuild -w sqlite3 -p

You have to build this native module with Electron based configurations.你必须使用基于 Electron 的配置来构建这个原生模块。

Try:尝试:
1. cd node_modules/sqlite3 1. cd node_modules/sqlite3
2. npm run prepublish 2. npm run prepublish
3. node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64 3. node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
4. node-gyp rebuild --target=1.3.1 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64 4. node-gyp rebuild --target=1.3.1 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64

This is assuming you have the very latest version of electron.这是假设您拥有最新版本的电子。 You can change the config to match your electron version.您可以更改配置以匹配您的电子版本。

If none of these are working try this.如果这些都不起作用,试试这个。

npm install electron-builder

Add this in your script tag of package.json file将此添加到 package.json 文件的脚本标记中

 "postinstall": "electron-builder install-app-deps"

Then execute this然后执行这个

npm run postinstall 

This saved a lot of my time这节省了我很多时间

1: Include rebuild in Package.json file and install npm electron-rebuild 1:在 Package.json 文件中包含重建并安装 npm electron-rebuild

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "rebuild": "electron-rebuild -f -w sqlite3"
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "author",
  "license": "CC0-1.0",
  "devDependencies": {
    "@types/file-saver": "0.0.1",
    "electron": "1.7",
    "electron-rebuild": "^1.6.0"
  },
  "dependencies": {
    "sqlite3": "^3.1.13"
  }
}

2: install python 2.7 and add its path to environmental variable eg C:\\Python27; 2:安装python 2.7并将其路径添加到环境变量例如C:\\Python27;

3: npm INSTALL and then npm run rebuild 3: npm INSTALL 然后 npm run rebuild

You have just installed the sqlite3 module but you need to rebuild it to run on a specific platform.您刚刚安装了sqlite3模块,但您需要重新构建它才能在特定平台上运行。 You'll need electron-rebuild package to rebuild the binary.您将需要electron-rebuild包来重建二进制文件。

Run Command npm i --save-dev electron-rebuild from your project directory.After Installing the ˚ electron-rebuild .从您的项目目录运行命令npm i --save-dev electron-rebuild 。安装 ˚ electron-rebuild Ru the following command to build sqlite3 binary for your platform.使用以下命令为您的平台构建 sqlite3 二进制文件。

./node_modules/.bin/electron-rebuild -w sqlite3 -p

If rebuild fails, run npm install and then run the above-given command once again.如果重建失败,请运行npm install然后再次运行上面给出的命令。

In my case, This solved the problem. 就我而言,这解决了问题。 It worked after build failed several time, The issue was only with windows pc. 构建失败几次后,它起作用了。问题仅在Windows pc上。 When i tried with mac, it worked at first without needing any further setup/installation process. 当我尝试使用Mac时,它最初不需要任何进一步的设置/安装过程即可工作。

npm install electron-rebuild
./node_modules/.bin/electron-rebuild -w sqlite3 -p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM