angular-electron icon indicating copy to clipboard operation
angular-electron copied to clipboard

Native node.js addons only loaded in main.ts (entrypoint)

Open sanchezpablo opened this issue 8 years ago • 7 comments

When trying to load native addons in components or services there is a problem with bindings, the error is:

Error: Could not load the bindings file. (and the tried paths)

the .node file is in the correct path, but the bindings module can't find it (I don't know why).

Sample code working on main.ts and not in any other place in the application: const addon = require('bindings')('addon.node'); console.log('This should be eight:' + addon.add(3, 5));

On the other hand, when doing the same thing in the main.ts (project entry point) of the application, the bindings are working like a charm.

It could be possible to solve this issue if some kind of webpack configuration provided? By the way, in this boilerplate, where is the webpack configuration file??? I can't find it.

How is this possible? Please any help will be welcomed! Thanks in advance!!!

sanchezpablo avatar Jul 03 '18 12:07 sanchezpablo

Finally found the reason:

in the components or services, instead of using require('bindings')('addon.node'), I should use window.require('bindings')('addon.node')

so simple....

sanchezpablo avatar Jul 03 '18 13:07 sanchezpablo

same problem here, also was wondering about the webpack configurations... can you please share example how you use the native module in a service? I can't get rid of this warning about binding. using serialport native node module:


 import * as SerialPort from 'serialport';
 
@Injectable()
 export class EdcComService {
  port: SerialPort;

  constructor() {
    this.port = new SerialPort(EdcComService.getCOMPort(), {
      baudRate: 9600,
      dataBits: 8,
      stopBits: 1,
      parity: 'none'
    }, (err: any): any => {
      if (err) {
        return console.log('Error creating port: ', err.message);
      }
    });

getting warnings : WARNING in ./node_modules/bindings/bindings.js 81:22-40 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/bindings/bindings.js 81:43-53 Critical dependency: the request of a dependency is an expression

and in chrome console the app dosen't load: Uncaught Error: Could not locate the bindings file. Tried: ..

@sanchezpablo thanks

shaniqwa avatar Jul 10 '18 07:07 shaniqwa

ok so I managed to get it done, so I removed the direct import from the service and replaced it with this.electronService.serialPort which requires the module correctly.


@Injectable()
export class EdcComService {
  port: any;

  constructor(private electronService: ElectronService) {
    this.port = new this.electronService.serialport(EdcComService.getCOMPort(), {
      baudRate: 9600,
      dataBits: 8,
      stopBits: 1,
      parity: 'none'
    }, (err: any): any => {
      if (err) {
        return console.log('Error creating port: ', err.message);
      }
    });

my app is running now :)

shaniqwa avatar Jul 10 '18 08:07 shaniqwa

this should be on readme

mtsunu avatar Jul 29 '18 20:07 mtsunu

@shaniqwa can you PLEASE help me with this? I can't get serialport to work in any way. How did you require the module in electron service???

renatop7 avatar Aug 22 '18 20:08 renatop7

@renatop7 this is my electron service, used electorn-angualr seed which explains this part. so any native node module you want to use should be imported here, then you can inject this ElectronService in any other service who needs the native modules, like EdcComService.

import { Injectable } from '@angular/core';
import { ipcRenderer } from 'electron';
import * as ipcMain from 'electron';
import * as childProcess from 'child_process';
import * as serialport from 'serialport';
import * as serialportElectron from 'serialport-builds-electron';

@Injectable()
export class ElectronService {
  ipcRenderer: typeof ipcRenderer;
  ipcMain: typeof ipcMain;
  childProcess: typeof childProcess;
  serialport: typeof serialport;
  serialportElectron: typeof serialportElectron;

  fs: any;
  logger: any;
  appPath: string;
  appVersion: string;

  constructor() {
    // Conditional imports
    if (this.isElectron()) {
      const remote = window.require('electron').remote;

      this.ipcRenderer = window.require('electron').ipcRenderer;
      this.ipcMain = window.require('electron');
      this.childProcess = window.require('child_process');
      this.serialport = window.require('serialport');
      this.serialportElectron = window.require('serialport-builds-electron');

      this.fs = window.require('fs');

      this.appPath = remote.app.getPath('appData');
      this.appVersion = remote.app.getVersion();

      this.logger = remote.require('electron-log');
      this.logger.transports.file.level = 'info';
    }
  }

  isElectron = () => {
    return window && window.process && window.process.type;
  };

}

shaniqwa avatar Aug 23 '18 09:08 shaniqwa

@shaniqwa very nice! thanks!

renatop7 avatar Aug 23 '18 22:08 renatop7