question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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!!!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
shaniqwacommented, Aug 23, 2018

@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;
  };

}

1reaction
renatop7commented, Aug 23, 2018

@shaniqwa very nice! thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node --loader treats entrypoint as ESM when should ... - GitHub
When --loader is passed, node always tries to load entrypoint scripts as ESM, ignoring that package.json wants the file to be treated as...
Read more >
NodeJS: loading ES Modules and native addons in the same ...
I ran into a similar problem and fixed it this way: https://nodejs.org/api/module.html#module_module_createrequire_filename
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
It can load JSON modules, but an import assertion is required. It accepts only .js , .mjs , and .cjs extensions for JavaScript...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
Read more >
TypeScript and native ESM on Node.js - 2ality
import {start} from 'my-package/dist/src/main.js';. The key point is that, by default, we need to provide filename extensions, especially for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found