Using NodeJS plugins in Electron

落爺英雄遲暮 提交于 2019-12-05 07:08:52

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/32406397/using-nodejs-plugins-in-electron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!