bitcoinjs / tiny-secp256k1

A tiny secp256k1 native/JS wrapper
MIT License
90 stars 55 forks source link

Must use import to load ES Module #62

Closed faustbrian closed 2 years ago

faustbrian commented 2 years ago

Upgrading to 3.0.0 of https://github.com/bitcoinjs/bip32 in a project that uses ESM is causing some issues when using the suggested tiny-secp256k1. When running tests via node --experimental-vm-modules node_modules/jest/bin/jest.js --forceExit "source/public-key.service.test.ts" an error about Must use import to load ES Module is thrown.

This seems to be caused by tiny-secp256k1 having declared its type as module in package.json since 2.0.0 which means you can no longer require it with certain tooling because they'll throw exceptions if you try to require anything that is declared as a module.

Is the suggested solution to stay on 1.0.0 of tiny-secp256k1 if you don't want to encounter this error? I saw there have been various bug fixes on 2.0.0 so it seems like an upgrade would be sensible.

 FAIL  source/public-key.service.test.ts
  ● Test suite failed to run

    Must use import to load ES Module: /Users/brianfaust/Work/sdk/common/temp/node_modules/.pnpm/tiny-secp256k1@2.1.0/node_modules/tiny-secp256k1/lib/index.js

      2 |
      3 | import BIP32Factory, { BIP32Interface } from "bip32";
    > 4 | import * as ecc from "tiny-secp256k1";
        | ^
      5 |
      6 | import { BIP39 } from "./bip39";
      7 |

      at Runtime.requireModule (../../common/temp/node_modules/.pnpm/jest-runtime@27.3.1/node_modules/jest-runtime/build/index.js:972:21)
      at Object.<anonymous> (../cryptography/source/bip32.ts:4:1)
      at Object.<anonymous> (../cryptography/source/index.ts:7:1)
junderw commented 2 years ago

ts-node, jest, and a loooooot of tooling is not supporting ESM yet... perhaps going pure ESM was a mistake...

I have successfully made a hybrid project with https://github.com/bitcoinjs/uint8array-tools

Perhaps I can do something similar with this project.

faustbrian commented 2 years ago

Guess the solution for now is to stay on v1 of tiny-secp256k1 if we don't want to run this issue. Doesn't seem to cause any issues for the bip32 package but still missing out on the bug fixes in v2 of tiny-secp256k1.

junderw commented 2 years ago

63 will fix this.

junderw commented 2 years ago

Fixed in v2.1.1

Both require and import can be used, and webpack etc. will use ESM for the browser.