简体   繁体   English

在Electron中使用NodeJS插件

[英]Using NodeJS plugins in Electron

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. 我是Electron(Atom-shell)的新手,我正在尝试将NodeJS插件加载到我正在构建的应用程序中,但我不知道如何。 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. 例如,我试图在我的应用程序中使用sqlite3插件,我使用npm install sqlite3 ,并且它已成功安装。 But the application throws and error when I try to call it var sqlite = require('sqlite3') . 但是当我尝试将其命名为var sqlite = require('sqlite3')时,应用程序抛出并出错。 Are there any further steps I am not aware of ? 还有其他我不知道的步骤吗?

Thanks. 谢谢。

For pure JS (ie not native) modules you need the following: 对于纯JS(即非本机)模块,您需要以下内容:

  1. Have the module listed in your package.json dependencies 将模块列在package.json依赖项中
  2. Let electron know where to find the module (eg export NODE_PATH=/PATH/TO/node_module ) 让电子知道在哪里找到模块(例如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. 对于使用C ++绑定的本机节点模块(例如sqlite3 ),您需要针对电子标头构建它们才能工作。 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

To install the npm modules correctly you should go into the folder of your electron app and install the module via npm. 要正确安装npm模块,您应该进入电子应用程序的文件夹并通过npm安装模块。

npm install --save sqlite3

The flag --save is important, because npm will install the module inside your app. 标志--save很重要,因为npm会在你的应用程序中安装模块。

Afterwards the require should work. 之后,要求应该有效。

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

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