Skip to content

Commit 21a99e1

Browse files
committed
fix(deps-dev): Dynamic import node:events
Avoids the following error when running `pnpm run test`: ``` test browser ℹ Browser "chromium" setup complete. ✘ [ERROR] Could not resolve "events" dist/test/utils/create-pubsub.js:1:32: 1 │ import { setMaxListeners } from 'events'; │ ~~~~~~~~ ╵ "./events" The package "events" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. ```
1 parent 60b5dad commit 21a99e1

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

test/utils/create-pubsub.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { setMaxListeners } from 'events'
21
import { generateKeyPair } from '@libp2p/crypto/keys'
32
import { TypedEventEmitter, start } from '@libp2p/interface'
43
import { mockRegistrar, mockConnectionManager, mockNetwork } from '@libp2p/interface-compliance-tests/mocks'
@@ -55,10 +54,14 @@ export const createComponents = async (opts: CreateComponentsOpts): Promise<Goss
5554

5655
mockNetwork.addNode(components)
5756

57+
let setMaxListeners: undefined | any;
5858
try {
5959
// not available everywhere
60-
setMaxListeners(Infinity, pubsub)
60+
setMaxListeners = (await import("node:events")).default.setMaxListeners;
6161
} catch {}
62+
if(typeof setMaxListeners != 'undefined') {
63+
setMaxListeners(Infinity, pubsub)
64+
}
6265

6366
return { pubsub, components }
6467
}

0 commit comments

Comments
 (0)