如何从电子中获取本机节点模块? ref 和 ffi 模块出错
How to require native node module from electron? Getting error with ref and ffi module
当我在我的 js 代码中需要 "ref" 模块并通过节点 运行 时,我得到了所需的输出。
但是当我通过在 package.json 中提供必要的更改通过电子 运行 相同的 js 代码时,它说 "Could not locate the binding file.."
这是我的 package.json 文件
{
"name": "firstapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"package": "electron-packager . myapp --platform=win32 --arch=ia32 --version=1.0.0 --overwrite"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^1.4.8",
"electron-prebuilt": "^1.4.8",
"electron-rebuild": "^1.4.0",
"ffi": "^2.2.0",
"node-gyp": "^3.4.0",
"reach": "^1.0.0",
"ref": "^1.3.3"
},
"dependencies": {
"ffi": "^2.2.0",
"ref": "^1.3.3"
}
}
这是我的 index.js 文件
const electron = require('electron');
const ref = require('ref');
const app = electron.app
const BrowserWindow = electron.BrowserWindow
let mainWindow
const createWindow = () => {
mainWindow = new BrowserWindow({ name: "ishwar", width: 800, height: 600, visible: true, toolbar: false });
mainWindow.loadURL(__dirname + '/index.html');
mainWindow.webContents.openDevTools()
mainWindow.on('closed', () => {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
app.quit()
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
当我在不需要 "ref" 的情况下启动项目 "npm start" 时,我没有收到任何错误。但是当我使用 ref 时,它会抛出错误。
P.S。主要要求是 "ffi" 模块,我已经对 运行 "ffi" 进行了必要的更改。 "ffi" 实习生需要 "ref",这样当我 运行 包含通过节点引用的代码时,它可以完美运行..
不知何故,我设法找到了这个问题的解决方案。使用 npm 命令安装这些包后,出现绑定错误。作为解决方案,我需要在外部为该新包执行 运行 "electron-rebuild" 命令。使用 npm 下载 electron-rebuild 模块,然后 运行 "electorn-rebuild -f -w ffi" 和 "electron-rebuild -f -w ref"。就是这样,现在可以使用了。
当我在我的 js 代码中需要 "ref" 模块并通过节点 运行 时,我得到了所需的输出。 但是当我通过在 package.json 中提供必要的更改通过电子 运行 相同的 js 代码时,它说 "Could not locate the binding file.."
这是我的 package.json 文件
{
"name": "firstapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"package": "electron-packager . myapp --platform=win32 --arch=ia32 --version=1.0.0 --overwrite"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^1.4.8",
"electron-prebuilt": "^1.4.8",
"electron-rebuild": "^1.4.0",
"ffi": "^2.2.0",
"node-gyp": "^3.4.0",
"reach": "^1.0.0",
"ref": "^1.3.3"
},
"dependencies": {
"ffi": "^2.2.0",
"ref": "^1.3.3"
}
}
这是我的 index.js 文件
const electron = require('electron');
const ref = require('ref');
const app = electron.app
const BrowserWindow = electron.BrowserWindow
let mainWindow
const createWindow = () => {
mainWindow = new BrowserWindow({ name: "ishwar", width: 800, height: 600, visible: true, toolbar: false });
mainWindow.loadURL(__dirname + '/index.html');
mainWindow.webContents.openDevTools()
mainWindow.on('closed', () => {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
app.quit()
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
当我在不需要 "ref" 的情况下启动项目 "npm start" 时,我没有收到任何错误。但是当我使用 ref 时,它会抛出错误。
P.S。主要要求是 "ffi" 模块,我已经对 运行 "ffi" 进行了必要的更改。 "ffi" 实习生需要 "ref",这样当我 运行 包含通过节点引用的代码时,它可以完美运行..
不知何故,我设法找到了这个问题的解决方案。使用 npm 命令安装这些包后,出现绑定错误。作为解决方案,我需要在外部为该新包执行 运行 "electron-rebuild" 命令。使用 npm 下载 electron-rebuild 模块,然后 运行 "electorn-rebuild -f -w ffi" 和 "electron-rebuild -f -w ref"。就是这样,现在可以使用了。