在 Electron 中使用 NodeJS 插件

Using NodeJS plugins in Electron(在 Electron 中使用 NodeJS 插件)
本文介绍了在 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. 让 electron 知道在哪里可以找到模块(例如 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)

第一个要求很明显,第二个要求源于this issue.

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

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

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 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
How to get insertId for MySQL using Mysql2 in Node with async and pool?(如何在带异步和池的Node中使用Mysql2获取MySQL的InsertID?)
How to make node.js mysql connection pooling available on startup(如何使node.js MySQL连接池在启动时可用)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)