LedgerHQ/ledgerjs

TransportNodeHID throwing error

directcuteo opened this issue · 0 comments

Hello!

Description

I have a NodeJS project where I implemented a Ledger sign functionality.
This project is created as a library and can be used standalone as a Node app, or with npm imported in an Angular Web Application.

In my file where I try to connect to the Ledger Device, I import both TransportNodeHID and TransportWebHID and I try to connect to the one that work for current context dynamically.

Code sample

  import TransportNodeHid from '@ledgerhq/hw-transport-node-hid';
  import TransportWebHID from '@ledgerhq/hw-transport-webhid';

  private async setTransport(): Promise<void> {
    if (!this.transport) {
      try {
        this.transport = await TransportNodeHid.create();
      } catch (e) {
        this.transport = undefined;
        console.error(e);
      }
    }
    if (!this.transport) {
      try {
        this.transport = await TransportWebHID.create();
      } catch (e) {
        this.transport = undefined;
        console.error(e);
      }
    }
  }

The Problem

The problem is when I import this project (as a dependency in the package.json) in my Angular App.
There is something Node specific in TransportNodeHid because I receive at application startup: 'require is not defined')

If I remove TransportNodeHid from the file everything works. What should be the way to go here so I can have this project handling both NodeJS and browser?
Thank you!