railsware / bozon

🛠 Command line tool for building, testing and publishing modern Electron applications
MIT License
758 stars 52 forks source link

how to include native module and usage of it in correct way. (newby question) #91

Closed iBehruz closed 2 years ago

iBehruz commented 2 years ago

I new in nodejs and webpack. so I'm in trouble about how to use specific module. I installed usb-detection module by following command 'yarn add usb-detection' and included it to my webpack.config.js by following an issue #88. my webpack.config.js file looks like:

const mode = 'development'; const BozonInjecterPlugin = require('./BozonPackageDependencyInjector');

module.exports = { renderer: { entry: './src/renderer/javascripts/index.js' }, preload: { entry: './src/preload/index.js' }, main: { externals: { "usb-detection" : 'commonjs usb-detection' }, entry: './src/main/index.js', plugins:[ new BozonInjecterPlugin({ src: ".", dest: ./builds/${mode}, dependencies:[ 'usb-detection' ]}) ] } },

and I attempted to use this module on my app. but i'm getting an error: VM75 renderer_init.js:93 TypeError: Cannot read property 'indexOf' of undefined at Function.getFileName (bindings.js:178) at bindings (bindings.js:82) at eval (index.js:13) at Object../nodemodules/usb-detection/index.js (VM88 C:\Frameworks\bozon\bozon\builds\development\preload\index.js:48) at webpack_require (VM88 C:\Frameworks\bozon\bozon\builds\development\preload\index.js:127) at eval (index.js:10) at Object../src/preload/index.js (VM88 C:\Frameworks\bozon\bozon\builds\development\preload\index.js:59) at webpack_require (VM88 C:\Frameworks\bozon\bozon\builds\development\preload\index.js:127) at VM88 C:\Frameworks\bozon\bozon\builds\development\preload\index.js:179 at Object. (VM88 C:\Frameworks\bozon_\bozon\builds\development\preload\index.js:181)

my preload.js: // https://electronjs.org/docs/tutorial/security // Preload File that should be loaded into browser window instead of // setting nodeIntegration: true for browser window

import { contextBridge, ipcRenderer } from 'electron'

const usb = require('usb-detection')

contextBridge.exposeInMainWorld('MessagesAPI', { onLoaded: callback => { ipcRenderer.on('loaded', callback) } })

contextBridge.exposeInMainWorld('usb', { detect: usb })

main.js

// In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here. import path from 'path' import { app, BrowserWindow} from 'electron'

const createWindow = () => { // Create the browser window. let win = new BrowserWindow({ title: CONFIG.name, width: CONFIG.width, height: CONFIG.height, webPreferences: { worldSafeExecuteJavaScript: true, preload: path.join(app.getAppPath(), 'preload', 'index.js') } })

// and load the index.html of the app. win.loadFile('renderer/index.html')

// send data to renderer process win.webContents.on('did-finish-load', () => { win.webContents.send('loaded', { appName: CONFIG.name, electronVersion: process.versions.electron, nodeVersion: process.versions.node, chromiumVersion: process.versions.modules }) })

win.on('closed', () => { win = null }) }

// This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.whenReady().then(createWindow)

// Quit when all windows are closed. app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } })

app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) { createWindow() } })

and i want to use it at least on browser console log like:

usb.detect.startMonitoring();

// Detect add/insert usb.detect.on('add', function(device) { console.log('add', device); });