paulmillr / scure-btc-signer

Audited & minimal library for creating, signing & decoding Bitcoin transactions.
https://paulmillr.com/noble/#scure
MIT License
135 stars 33 forks source link

import in node js throws error #32

Closed radicleart closed 1 year ago

radicleart commented 1 year ago

Hey, I'm struggling to import the library into my node js application. I've tried both micro-btc-signer and the new @scure/btc-signer packages.

node -v v19.8.0

E.g. for @scure library

npm install @scure/btc-signer
import btc from '@scure/btc-signer';

or trying dynamic imports;

const btc = await import('@scure/btc-signer');
or
const {TEST_NETWORK, NETWORK,Address,OutScript} = await import('@scure/btc-signer')

gives persistent error;

Error [ERR_REQUIRE_ESM]: require() of ES Module ../node_modules/micro-btc-signer/index.js from .../bridge-api/src/lib/bitcoin/rpc_transaction.ts not supported.

I imagine it's something misconfigured my end but I've have been bashing my head against it for several hours, so raising in case its a known issue?

paulmillr commented 1 year ago

You are probably using common.js modules. We don't support these. You need to switch to ESM. Also there is no default export, you need to use star:

import * as  btc from '@scure/btc-signer';
radicleart commented 1 year ago

Removed al references to CommonjJS as suggested and the import worked.

Thanks!