How to use sqlite3 module with electron?

前端 未结 10 1335
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 01:43

I want to develop desktop app using electron that uses sqlite3 package installed via npm with the command

npm install --save sqlite3

but it

相关标签:
10条回答
  • 2020-11-28 02:21
    npm install --save sqlite3
    npm install --save-dev electron-rebuild
    

    Then, in the scripts of your package.json, add this line:

    "scripts": {
      "postinstall": "electron-rebuild",
      ...
    },
    

    Then just re-install to trigger the post-install:

    npm install
    

    Works flawlessly for me in a complex use case also involving electron-builder, electron-webpack and sequelize.

    It works in electron-webpack's dev mode and in production mode for both Windows and Linux.

    0 讨论(0)
  • 2020-11-28 02:23

    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.

    0 讨论(0)
  • 2020-11-28 02:25

    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
    
    0 讨论(0)
  • 2020-11-28 02:26

    You can manually build the native modules using visual studio.

    1. Download visual studio 2019.
    2. Install package "desktop development with c++". In installation details tab select "MSVC v140 - VS 2015 C++ build tools (v14.00)"
    3. Download electron-builder in your project.
    4. In package.json create a script. "scripts": { "postinstall": "install-app-deps" }

    5. then run the script.

    0 讨论(0)
  • 2020-11-28 02:32

    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/

    0 讨论(0)
  • 2020-11-28 02:34

    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:

    • Electron App with Database
    • Using NodeJS plugins in Elelectron

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

    0 讨论(0)
提交回复
热议问题