diff --git a/README.md b/README.md index f855e77..fed1651 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ import "websocket-polyfill"; // or: require('websocket-polyfill'); ``` -if you get an `crypto is not defined` error you have to import it first: +if you get an `crypto is not defined` error, either upgrade to node.js 20 or above, or import it manually: ```js import * as crypto from 'crypto'; // or 'node:crypto' diff --git a/examples/crypto-polyfill.js b/examples/crypto-polyfill.js new file mode 100644 index 0000000..825491a --- /dev/null +++ b/examples/crypto-polyfill.js @@ -0,0 +1,4 @@ +import * as crypto from "node:crypto"; // required in node.js < 20 +if (!global.crypto) { + global.crypto = crypto; +} diff --git a/examples/nwc/client/get-balance.js b/examples/nwc/client/get-balance.js index 9bcbacc..7d70c0c 100644 --- a/examples/nwc/client/get-balance.js +++ b/examples/nwc/client/get-balance.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/get-info.js b/examples/nwc/client/get-info.js index f901e16..2a5aced 100644 --- a/examples/nwc/client/get-info.js +++ b/examples/nwc/client/get-info.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/get-wallet-service-info.js b/examples/nwc/client/get-wallet-service-info.js index 15a20ad..bb5dc2f 100644 --- a/examples/nwc/client/get-wallet-service-info.js +++ b/examples/nwc/client/get-wallet-service-info.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/get-wallet-service-supported-methods.js b/examples/nwc/client/get-wallet-service-supported-methods.js index f724272..de03b85 100644 --- a/examples/nwc/client/get-wallet-service-supported-methods.js +++ b/examples/nwc/client/get-wallet-service-supported-methods.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/list-transactions.js b/examples/nwc/client/list-transactions.js index e173e16..da2a140 100644 --- a/examples/nwc/client/list-transactions.js +++ b/examples/nwc/client/list-transactions.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/lookup-invoice.js b/examples/nwc/client/lookup-invoice.js index 5b5f9e5..40d1529 100644 --- a/examples/nwc/client/lookup-invoice.js +++ b/examples/nwc/client/lookup-invoice.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/make-invoice.js b/examples/nwc/client/make-invoice.js index 923de1b..6b9d520 100644 --- a/examples/nwc/client/make-invoice.js +++ b/examples/nwc/client/make-invoice.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/multi-pay-invoice.js b/examples/nwc/client/multi-pay-invoice.js index c6e6e8a..a0c4805 100644 --- a/examples/nwc/client/multi-pay-invoice.js +++ b/examples/nwc/client/multi-pay-invoice.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import { LightningAddress } from "@getalby/lightning-tools"; diff --git a/examples/nwc/client/multi-pay-keysend.js b/examples/nwc/client/multi-pay-keysend.js index 75907de..cffaffd 100644 --- a/examples/nwc/client/multi-pay-keysend.js +++ b/examples/nwc/client/multi-pay-keysend.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/pay-invoice.js b/examples/nwc/client/pay-invoice.js index 147b0cd..cabfdb9 100644 --- a/examples/nwc/client/pay-invoice.js +++ b/examples/nwc/client/pay-invoice.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/pay-keysend.js b/examples/nwc/client/pay-keysend.js index f2b52f2..af8d249 100644 --- a/examples/nwc/client/pay-keysend.js +++ b/examples/nwc/client/pay-keysend.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/sign-message.js b/examples/nwc/client/sign-message.js index 06138b3..e4566e3 100644 --- a/examples/nwc/client/sign-message.js +++ b/examples/nwc/client/sign-message.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/client/subscribe.js b/examples/nwc/client/subscribe.js index 588962f..20b970b 100644 --- a/examples/nwc/client/subscribe.js +++ b/examples/nwc/client/subscribe.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/get-balance.js b/examples/nwc/get-balance.js index 1e77cd3..0f25caa 100644 --- a/examples/nwc/get-balance.js +++ b/examples/nwc/get-balance.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/get-info.js b/examples/nwc/get-info.js index 3e49b7e..1daffda 100644 --- a/examples/nwc/get-info.js +++ b/examples/nwc/get-info.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/keysend.js b/examples/nwc/keysend.js index 9dfe6ef..2317af6 100644 --- a/examples/nwc/keysend.js +++ b/examples/nwc/keysend.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/list-transactions.js b/examples/nwc/list-transactions.js index 7f1e911..0118502 100644 --- a/examples/nwc/list-transactions.js +++ b/examples/nwc/list-transactions.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/lookup-invoice.js b/examples/nwc/lookup-invoice.js index dfe7ff7..2f4209e 100644 --- a/examples/nwc/lookup-invoice.js +++ b/examples/nwc/lookup-invoice.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/make-invoice.js b/examples/nwc/make-invoice.js index e147cac..1d914d6 100644 --- a/examples/nwc/make-invoice.js +++ b/examples/nwc/make-invoice.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/multi-keysend.js b/examples/nwc/multi-keysend.js index ab27a9e..78d0581 100644 --- a/examples/nwc/multi-keysend.js +++ b/examples/nwc/multi-keysend.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/send-multi-payment.js b/examples/nwc/send-multi-payment.js index a02bf5c..1138871 100644 --- a/examples/nwc/send-multi-payment.js +++ b/examples/nwc/send-multi-payment.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import { LightningAddress } from "@getalby/lightning-tools"; diff --git a/examples/nwc/send-payment.js b/examples/nwc/send-payment.js index bc1c425..bc203c3 100644 --- a/examples/nwc/send-payment.js +++ b/examples/nwc/send-payment.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises"; diff --git a/examples/nwc/sign-message.js b/examples/nwc/sign-message.js index bea8631..5eb2441 100644 --- a/examples/nwc/sign-message.js +++ b/examples/nwc/sign-message.js @@ -1,5 +1,4 @@ -import * as crypto from "node:crypto"; // required in node.js -global.crypto = crypto; // required in node.js +import "../crypto-polyfill.js"; import "websocket-polyfill"; // required in node.js import * as readline from "node:readline/promises";