Issue w/ Opus -> Ogg recording voice with prism-media@v2.0.0-alpha.0 - ERR_REQUIRE_ESM

I was trying to follow this example recording voice demo (https://github.com/discordjs/voice-examples/blob/main/recorder/src/createListeningStream.ts#L19C1-L27C5), but I'm running into the following error:
๐Ÿ‘‚ Listening to dimsey8_0
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/user/Desktop/unicorn/Voice/node_modules/node-crc/lib/lib.js from /Users/user/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js not supported.
Instead change the require of lib.js in /Users/user/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js to a dynamic import() which is available in all CommonJS modules.
}
๐Ÿ‘‚ Listening to dimsey8_0
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/user/Desktop/unicorn/Voice/node_modules/node-crc/lib/lib.js from /Users/user/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js not supported.
Instead change the require of lib.js in /Users/user/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js to a dynamic import() which is available in all CommonJS modules.
}
I am using ESM instead of CommonJS (the example link uses CommonJS). As a result, despite prism being ESM compatible, an underlying part of it, e.g., node-crc seems to be using CJS. I'm not sure how to go about fixing this. Any ideas would be great. I was thinking of looking for another stream encoder for Opus -> Ogg but I haven't found any that work yet. Does anyone have advice for me? Thank you in advance. Code will be pasted as a comment.
GitHub
voice-examples/recorder/src/createListeningStream.ts at main ยท disc...
A collection of examples of how to use @discordjs/voice in your projects - discordjs/voice-examples
6 Replies
d.js toolkit
d.js toolkitโ€ข12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - โœ… Marked as resolved by staff
dimsey
dimseyโ€ข12mo ago
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream';
import prism from 'prism-media';

function getDisplayName(userId: string, user?: User) {
return user ? `${user.username}_${user.discriminator}` : userId;
}

export function createListeningStream(receiver: VoiceReceiver, userId: string, user?: User) {

console.log(`๐Ÿ‘‚ Listening to ${getDisplayName(userId, user)}`);
const sourceOpusStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000, // milliseconds of silence before ending? TODO: (sr) figure out what this does
},
});

const filename = `./recordings/AA-${Date.now()}-${getDisplayName(userId, user)}.ogg`; // TODO: (sr) make this a .ogg file once we have an encoder

const destinationStream = createWriteStream(filename);

const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
channelCount: 2,
sampleRate: 48000,
}),
pageSizeControl: {
maxPackets: 10,
},
});

// TODO: (sr) Need an OPUS to OGG encoder between the two
pipeline(sourceOpusStream, oggStream, destinationStream, (err) => {
if (err) {
console.error('Pipeline error:', err);
} else {
console.log('Pipeline complete');
}
});
}
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream';
import prism from 'prism-media';

function getDisplayName(userId: string, user?: User) {
return user ? `${user.username}_${user.discriminator}` : userId;
}

export function createListeningStream(receiver: VoiceReceiver, userId: string, user?: User) {

console.log(`๐Ÿ‘‚ Listening to ${getDisplayName(userId, user)}`);
const sourceOpusStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000, // milliseconds of silence before ending? TODO: (sr) figure out what this does
},
});

const filename = `./recordings/AA-${Date.now()}-${getDisplayName(userId, user)}.ogg`; // TODO: (sr) make this a .ogg file once we have an encoder

const destinationStream = createWriteStream(filename);

const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
channelCount: 2,
sampleRate: 48000,
}),
pageSizeControl: {
maxPackets: 10,
},
});

// TODO: (sr) Need an OPUS to OGG encoder between the two
pipeline(sourceOpusStream, oggStream, destinationStream, (err) => {
if (err) {
console.error('Pipeline error:', err);
} else {
console.log('Pipeline complete');
}
});
}
discord.js@14.11.0 + node@v18.18.0
Stack trace
๐Ÿ‘‚ Listening to dimsey8_0
/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/sr/Desktop/unicorn/Voice/node_modules/node-crc/lib/lib.js from /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js not supported.
Instead change the require of lib.js in /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js to a dynamic import() which is available in all CommonJS modules.
at require.extensions.<computed> [as .js] (/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851:20)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js:50:19)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/opus/OggLogicalBitstream.js:13:9)
at createListeningStream (file:///Users/sr/Desktop/unicorn/Voice/src/utils/createListeningStream.ts:18:23)
at Object.<anonymous> (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:42:21)
at Generator.next (<anonymous>)
at fulfilled (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:4:58) {
code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...
Stack trace
๐Ÿ‘‚ Listening to dimsey8_0
/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/sr/Desktop/unicorn/Voice/node_modules/node-crc/lib/lib.js from /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js not supported.
Instead change the require of lib.js in /Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js to a dynamic import() which is available in all CommonJS modules.
at require.extensions.<computed> [as .js] (/Users/sr/Desktop/unicorn/Voice/node_modules/ts-node/dist/index.js:851:20)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/ogg/OggLogicalBitstream.js:50:19)
at new OggLogicalBitstream (/Users/sr/Desktop/unicorn/Voice/node_modules/prism-media/dist/opus/OggLogicalBitstream.js:13:9)
at createListeningStream (file:///Users/sr/Desktop/unicorn/Voice/src/utils/createListeningStream.ts:18:23)
at Object.<anonymous> (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:42:21)
at Generator.next (<anonymous>)
at fulfilled (file:///Users/sr/Desktop/unicorn/Voice/src/commands/join.ts:4:58) {
code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...
duck
duckโ€ข12mo ago
the version of node-crc that example should be using uses cjs, yes but your error indicates that node-crc appears to be using esm, which is the case on newer major versions please ensure that you haven't strayed from the example's dependencies if your package.json looks correct, you may just need to reinstall deps
dimsey
dimseyโ€ข12mo ago
Thank you so much! That was my issue--my crc-node version was v3 when the documentation uses an older version. Also sending you nitro b/c that just saved me hours and hours @duck Do you mind adding me or something so I can send you the link? I could put it here but then anyone can see it or i can paste here quickly lol
duck
duckโ€ข12mo ago
I'm actually good, thanks like everyone else helping, I'm just here as a volunteer
dimsey
dimseyโ€ข12mo ago
Ok! Well thank you so much, highlight of my day
Want results from more Discord servers?
Add your server