Replying to Avatar liminal 🦠

nostr:npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft

nostr:npub1zuuajd7u3sx8xu92yav9jwxpr839cs0kc3q6t56vd5u9q033xmhsk6c2uc I'm clearly missing something simple here. Can't find any example that uses NDKPrivateKeySigner either.

```

import NDK, {

NDKEvent,

NDKFilter,

NDKPrivateKeySigner,

} from "@nostr-dev-kit/ndk";

const signer = NDKPrivateKeySigner.generate();

console.log(signer);

const ndk = new NDK({

explicitRelayUrls: ["wss://relay.damus.io", "wss://relay.nostr.band"],

signer: signer,

});

const ndkEvent = new NDKEvent(ndk);

ndkEvent.kind = 1;

ndkEvent.content = "Hello, world!";

ndkEvent.created_at = Math.floor(Date.now() / 1000);

ndk.connect().then(() => {

ndkEvent.publish().then(() => {

console.log("Event published!");

});

});

```

```

file:///home/user/Documents/Programming/upload_files/node_modules/.pnpm/@nostr-dev-kit+ndk@0.6.5_typescript@5.5.2/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:670

this.sig = await _signer.sign(nostrEvent);

^

TypeError: Cannot read properties of undefined (reading 'sign')

at NDKEvent.sign (file:///home/user/Documents/Programming/upload_files/node_modules/.pnpm/@nostr-dev-kit+ndk@0.6.5_typescript@5.5.2/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:670:30)

at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

at async NDKEvent.publish (file:///home/user/Documents/Programming/upload_files/node_modules/.pnpm/@nostr-dev-kit+ndk@0.6.5_typescript@5.5.2/node_modules/@nostr-dev-kit/ndk/dist/index.mjs:679:7)

```

hmm, that looks ok at first glance; I’ll copy the code and run it, there must be something obvious

Worth trying, right after creating the signer:

signer.user().then(console.log)

there’s gotta be something going on instantiating the signer; in what environment is this running?

Reply to this note

Please Login to reply.

Discussion

pnpm and I'm running Fedora.

but are you running in a browser? node?

Yes using node.

I'm going to guess you are missing a websocket-polyfill

I just literally copied your code without changing a thing and it just published nevent1qgspdq8u9akd245y7u5hxtthvcsjd4g54uzcrp7dnlx9ehxyfgyyj0cpzamhxue69uhhyetvv9ujumn0wd68ytnzv9hxgtcpz4mhxue69uhhyetvv9ujuerpd46hxtnfduhsqg9acv82a30e0v6lkhf05f7rmjzhaxnxa7v86y6asa2k6d7s6g4q5uu639fp

make sure to import "websocket-polyfill" if you are running a nodejs environment

Getting an errror from the import, even from a new project

pnpm install --save websocket-polyfill

```

import "websocket-polyfill"

```

npx esrun test.ts

```

/home/user/Documents/Programming/upload/node_modules/.pnpm/websocket-polyfill@1.0.0_bufferutil@4.0.8_utf-8-validate@5.0.10/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. (/home/user/Documents/Programming/alexandria_upload/node_modules/.pnpm/websocket-polyfill@1.0.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/websocket-polyfill/src/index.ts:5:40)

at Module._compile (node:internal/modules/cjs/loader:1369:14)

at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)

at Module.load (node:internal/modules/cjs/loader:1206:32)

at Module._load (node:internal/modules/cjs/loader:1022:12)

at cjsLoader (node:internal/modules/esm/translators:366:17)

at ModuleWrap. (node:internal/modules/esm/translators:315:7)

at ModuleJob.run (node:internal/modules/esm/module_job:222:25)

at async ModuleLoader.eval (node:internal/modules/esm/loader:212:24)

at async loadESM (node:internal/process/esm_loader:28:7)

```

completely forgot about the websocket-polyfill, removed from the nostr-tools readme. Now recommends this:

``

import { useFetchImplementation } from 'nostr-tools/nip05'

useFetchImplementation(require('node-fetch'))

``

Adding that to ndk fixed it. Thanks!

```

import { useWebSocketImplementation } from 'nostr-tools/pool'

// or import { useWebSocketImplementation } from 'nostr-tools/relay' if you're using the Relay directly

import WebSocket from 'ws'

useWebSocketImplementation(WebSocket)

```

copied wrong snippet.