Development Tip

전자와 함께 sqlite3 모듈을 사용하는 방법은 무엇입니까?

yourdevel 2020. 11. 10. 22:23
반응형

전자와 함께 sqlite3 모듈을 사용하는 방법은 무엇입니까?


npm을 통해 설치된 sqlite3 패키지를 명령으로 사용 하는 전자 를 사용 하여 데스크톱 앱을 개발하고 싶습니다.

npm install --save sqlite3

그러나 전자 브라우저 콘솔에서 다음 오류가 발생합니다.

Uncaught Error: Cannot find module 'E:\allcode\eapp\node_modules\sqlite3\lib\binding\node-v45-win32-x64\node_sqlite3.node'

내 개발 환경은 Windows 8.1 x64 노드 버전 12.7입니다.

package.json 파일은 다음과 같습니다.

{
  "name": "eapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron-prebuilt": "^0.32.1"
  },
  "dependencies": {
    "angular": "^1.3.5",   
    "sqlite3": "^3.1.0"
  }
}

index.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() {
    // Create the browser window.
    mainWindow = new BrowserWindow({width: 800, height: 600}); 
    mainWindow.loadUrl('file://' + __dirname + '/index.html');   
    mainWindow.openDevTools();  
    mainWindow.on('closed', function() {       
        mainWindow = null;
    });
});

my.js 파일

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

db.serialize(function() {
    db.run("CREATE TABLE if not exists 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();

index.html 파일

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div >
    <div>
        <h2>Hello</h2>
    </div>

</div>
<!--<script src="js/jquery-1.11.3.min.js"></script>-->
<script src="js/my.js"></script>
</body>
</html>

전자와 함께 SQLite를 사용하는 가장 쉬운 방법은 electron-builder.

먼저 package.json에 설치 후 단계를 추가합니다.

"scripts": {
   "postinstall": "install-app-deps"
   ...
}

그런 다음 필요한 종속성을 설치하고 빌드하십시오.

npm install --save-dev electron-builder
npm install --save sqlite3
npm run postinstall

electron-builder는 Electron 바인딩에 대한 올바른 이름으로 플랫폼 용 네이티브 모듈을 빌드합니다. 그런 다음 require정상적으로 코드에서 할 수 있습니다 .

See my github repo and blog post - it took me quite a while to figure this out too.


I would not recommend the native node sqlite3 module. It requires being rebuild to work with electron. This is a massive pain to do - At least I can never get it to work and their a no instructions to for rebuilding modules on windows.

Instead have a look at kripken's 'sql.js' module which is sqlite3 that has been compiled 100% in JavaScript. https://github.com/kripken/sql.js/


Two aspects are to be considered here:

  1. Setting NODE_PATH: this lets electron know where to find your modules (see this answer for a thorough explanation)
  2. Compiling native modules against electron headers: see official docs

And checkout the following questions, that ask the same thing:


My tip would be to give lovefield (by Google) a try.


A simpler solution:

  1. Install electron-rebuild npm i electron-rebuild --save-dev
  2. Launch electron-rebuild ./node_modules/.bin/electron-rebuild (or .\node_modules\.bin\electron-rebuild.cmd on windows)
  3. Go to "node_modules/sqlite3/lib/binding/" and rename the folder "electron-v0.36-darwin-x64" to "node-v47-darwin-x64"

PS: v47 is my version, be careful to choose the good one (in your case v45)


I was having same problem. Tried everything and atlast this worked for me :-

npm install --save sqlite3
npm install --save electron-rebuild
npm install --save electron-prebuilt
.\node_modules\.bin\electron-rebuild.cmd

This will create "electron-v1.3-win32-x64" folder in .\node_modules\sqlite3\lib\binding\ location which is used by electron to use sqlite3.

Just start application and you will be able to use sqlite3 now.


I encounter this error too. Here is how i solve it: npm install --save-dev electron-rebuild then: ./node_modules/.bin/electron-rebuild

from: https://electronjs.org/docs/tutorial/using-native-node-modules

ps: While it's on rebuilding, don't use npm startto lanch the electron app. Otherwise the rebuild process would fail.


Have a look at a similar answer here

TL;DR

cd .\node_modules\sqlite3
npm install nan --save
npm run prepublish
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64
node-gyp rebuild --target=1.3.2 --arch=x64 --target_platform=win32 --dist-url=http://electron.atom.io/ --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.3-win32-x64

It works for me in version 3 and 4, unfortunately NOT version 5. See the sqlite3 documentation for details: https://www.npmjs.com/package/sqlite3#custom-builds-and-electron or otherwise run the following line: npm install sqlite3 --runtime=electron --target=4.0.0 --dist-url=https://atom.io/download/electron

참고URL : https://stackoverflow.com/questions/32504307/how-to-use-sqlite3-module-with-electron

반응형