2

I am new to Electron (Atom-shell), and I am trying to load a NodeJS plugin into the application I am building, but I don't know how. The documentation is not clear on that.

For instance, I am trying to use sqlite3 plugin in my app, I used npm install sqlite3, and it was successfully installed. But the application throws and error when I try to call it var sqlite = require('sqlite3'). Are there any further steps I am not aware of ?

Thanks.

Leonardo Buscemi
  • 1,111
  • 1
  • 15
  • 22
Dewan159
  • 2,984
  • 7
  • 39
  • 43

2 Answers2

7

For pure JS (i.e. not native) modules you need the following:

  1. Have the module listed in your package.json dependencies
  2. Let electron know where to find the module (e.g. export NODE_PATH=/PATH/TO/node_module)

The first requirement is obvious and the second has its roots in this issue.

For native node modules (such as sqlite3) which use C++ bindings, you need to build them against electron headers to work. According to electron docs, the easiest way to do that would be:

npm install --save-dev electron-rebuild

# Every time you run npm install, run this
./node_modules/.bin/electron-rebuild
Yan Foto
  • 10,850
  • 6
  • 57
  • 88
  • 1
    Thanks, that clears so much for me. Right now I am struggling with the "gyp" thing, more troubles with the python version and the C++ installation. I cannot understand why on earth thing are this hard. That's what is wrong with open-source stuff :( – Dewan159 Sep 05 '15 at 21:31
  • 2
    Glad I could help! Creating cross platform applications is hard, no matter open source or not. Take a look at [this article](http://quaintous.com/2015/06/12/node-gyp-for-non-cpp-programmers/), it might help you with node-gyp. – Yan Foto Sep 06 '15 at 00:37
1

To install the npm modules correctly you should go into the folder of your electron app and install the module via npm.

npm install --save sqlite3

The flag --save is important, because npm will install the module inside your app.

Afterwards the require should work.

apxp
  • 5,240
  • 4
  • 23
  • 43
  • Thanks, but as it turned out, this works fine only with non-native node module, the native ones require further work (Installing python and Visual Studio for some reason). – Dewan159 Sep 05 '15 at 21:33