在Electron中使用NodeJS插件 [英] Using NodeJS plugins in Electron

查看:786
本文介绍了在Electron中使用NodeJS插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Electron(Atom-shell)不熟悉,我正在尝试将NodeJS插件加载到正在构建的应用程序中,但是我不知道该怎么做。文档尚不清楚。

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.

例如,我尝试在我的应用中使用sqlite3插件,我使用了 npm install sqlite3 ,并且已成功安装。但是,当我尝试将其命名为 var sqlite = require('sqlite3')时,应用程序会抛出错误。

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 ?

谢谢。

推荐答案

对于纯JS(即非本机)模块,您需要以下内容:

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


  1. 模块已列在 package.json 依赖项

  2. 让电子知道在哪里找到模块(例如 export NODE_PATH = / PATH / TO / node_module

  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.

对于本机节点模块(例如 sqlite3 )使用C ++绑定,您需要针对电子标头构建它们才能工作。根据电子文档,最简单的方法是:

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

这篇关于在Electron中使用NodeJS插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆