nostr-dev-kit / ndk

Nostr Development Kit with outbox-model support
https://nostr-dev-kit.github.io/ndk/
MIT License
366 stars 99 forks source link

Failed to connect ReferenceError: WebSocket is not defined #189

Closed insanity54 closed 8 months ago

insanity54 commented 8 months ago

I'm brand new to NOSTR and I would like to use ndk to run some NIP-10 experiments.

I'm having some trouble getting started. I'm using node 18.19.1 and here is my script.

import 'dotenv/config'
import NDK from "@nostr-dev-kit/ndk";

const pk = process.env.NOSTR_PUBLIC_KEY
const sk = process.env.NOSTR_SECRET_KEY

if (!pk) throw new Error('NOSTR_PUBLIC_KEY missing in env');
if (!sk) throw new Error('NOSTR_SECRET_KEY missing in env');

const ndk = new NDK({
  explicitRelayUrls: [
      "wss://relay.snort.social",
  ],
  enableOutboxModel: false,
});

await ndk.connect(6000);

My .env file has NOSTR_PUBLIC_KEY and NOSTR_SECRET_KEY defined using keys I generated on Primal. It also has DEBUG='ndk:*'

When I start this program, I see errors.

node index.js                                                              ✔  6s  
  ndk:pool Connecting to 1 relays, timeout 6000... +0ms
  ndk:relay:wss://relay.snort.social:connectivity Failed to connect ReferenceError: WebSocket is not defined
    at file:///home/chris/Documents/nostrbot/node_modules/.pnpm/nostr-tools@1.17.0/node_modules/nostr-tools/lib/esm/index.js:413:9
    at new Promise (<anonymous>)
    at connectRelay (file:///home/chris/Documents/nostrbot/node_modules/.pnpm/nostr-tools@1.17.0/node_modules/nostr-tools/lib/esm/index.js:411:25)
    at Object.connect (file:///home/chris/Documents/nostrbot/node_modules/.pnpm/nostr-tools@1.17.0/node_modules/nostr-tools/lib/esm/index.js:518:11)
    at NDKRelayConnectivity.connect (file:///home/chris/Documents/nostrbot/node_modules/.pnpm/@nostr-dev-kit+ndk@2.5.0/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:73:24)
    at NDKRelay.connect (file:///home/chris/Documents/nostrbot/node_modules/.pnpm/@nostr-dev-kit+ndk@2.5.0/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:742:30)
    at NDKPool.connect (file:///home/chris/Documents/nostrbot/node_modules/.pnpm/@nostr-dev-kit+ndk@2.5.0/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:5344:31)
    at NDK.connect (file:///home/chris/Documents/nostrbot/node_modules/.pnpm/@nostr-dev-kit+ndk@2.5.0/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:5803:36)
    at file:///home/chris/Documents/nostrbot/index.js:26:11
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25) +0ms
  ndk:pool Failed to connect to relay wss://relay.snort.social: ReferenceError: WebSocket is not defined +4ms

I did Ctrl+F on https://github.com/nostr-dev-kit/ndk and I didn't see any mention of websockets. I looked in the examples dir and found hello world which imports websocket-polyfill.

So I tried importing that myself.

import 'dotenv/config'
import NDK from "@nostr-dev-kit/ndk";
import 'websocket-polyfill'

const pk = process.env.NOSTR_PUBLIC_KEY
const sk = process.env.NOSTR_SECRET_KEY
if (!pk) throw new Error('NOSTR_PUBLIC_KEY missing in env');
if (!sk) throw new Error('NOSTR_SECRET_KEY missing in env');
const ndk = new NDK({
  explicitRelayUrls: [
      "wss://relay.snort.social",
  ],
  enableOutboxModel: false,
});
await ndk.connect(6000);

More errors seen.

node index.js
/home/chris/Documents/nostrbot/node_modules/.pnpm/websocket-polyfill@1.0.0/node_modules/websocket-polyfill/lib/index.js:8
    (_a = (_b = global).WebSocket) !== null && _a !== void 0 ? _a : (_b.WebSocket = (0, import2_1.default)("ws"));
                                                                                                          ^

TypeError: (0 , import2_1.default) is not a function
    at Object.<anonymous> (/home/chris/Documents/nostrbot/node_modules/.pnpm/websocket-polyfill@1.0.0/node_modules/websocket-polyfill/lib/index.js:8:107)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:106:12)

Node.js v18.19.1

Any suggestions here?

erskingardner commented 8 months ago

@insanity54 I saw this yesterday on a project I was updating dependencies for. websocket-polyfill version 1.0.0 is a deprecation of the library and tries to shim in some default browser behavior in a different way. I got the same error as you're seeing when I tried to update. I haven't had time to dig into what the reason behind it is but in the meantime, if you downgrade that library to 0.0.3 it should work just fine for you.

insanity54 commented 8 months ago

Oh wonderful! websocket-polyfill@0.0.3 worked for me. Many thanks!