bitcoinjs / bip32

A BIP32 compatible library.
MIT License
181 stars 129 forks source link

TypeError: BIP32Factory is not a function #53

Closed pajasevi closed 2 years ago

pajasevi commented 2 years ago

I am using a documented moethod of using this library, yet Node.js throws an error when I try to use the BIP32Factory.

import BIP32Factory from 'bip32'
import * as ecc from 'tiny-secp256k1'

const bip32 = BIP32Factory(ecc)

results in error:

TypeError: BIP32Factory is not a function

My project is running Node.js v14.17.5 and is configured to use ES modules. Same error happened when I tried importing tiny-secp256k1 asynchronously.

I've also tried importing the module directly as import BIP32Factory from 'bip32/src/bip32.js' but it resulted in the same error.

I' ve looked into the source files (in node_modules) and haven't found what could be wrong.

pajasevi commented 2 years ago

Found out this can be somewhat "fixed" when I use the module as

const bip32 = BIP32Factory.default(ecc)

Which seems to me like there is something wrong when I'm actually using the ES modules.

junderw commented 2 years ago

bip32 doesn't support esm yet.

the import example is for Typescript.

pajasevi commented 2 years ago

Yeah but the weird thing is that I haven't experienced such issue with any other library. Which kinda tells me there might be something wrong with the TS export transpilation to JS.

junderw commented 2 years ago

esModuleInterop maybe?

can you try:

Tell me if the issue resolves.

pajasevi commented 2 years ago

That didn't actually help. I also tried messing with reexporting in ts-src/index.ts but with no luck. Always leads to the same compiled code.

electic commented 2 years ago

Getting the same error:

This expression is not callable.
  Type 'typeof import("/Users/x/Documents/x/code/x/x/node_modules/bip32/types/index")' has no call signatures.ts(2349)
pajasevi commented 2 years ago

Found out this is an issue in Typescript code as well because the generated types are different than generated code. Only way to fix this was using ts-ignore

Snímek obrazovky 2021-11-16 v 18 07 13

pajasevi commented 2 years ago

Fixed in 3.0.1

junderw commented 2 years ago

I also have a PR for tiny-secp256k1 that Allows commonJS usage (require)

If you have time, please take a look.

https://github.com/bitcoinjs/tiny-secp256k1/pull/63