Skip to content

Weird error caused by only importing undici module. #10946

Open
@rafinetiz

Description

@rafinetiz

Which package is this bug report for?

discord.js

Issue description

my project is also using undici as http client.
weirdly, by only importing undici module you cannot send any message contains a files/attachment.

stack trace

TypeError: Cannot read properties of null (reading 'byteLength')
    at Object.pull (C:\Users\Angger\Desktop\project\dcjs-reproduce\node_modules\.pnpm\[email protected]\node_modules\undici\lib\web\fetch\body.js:63:20)
    at invokePromiseCallback (node:internal/webstreams/util:172:10)
    at Object.<anonymous> (node:internal/webstreams/util:177:23)
    at readableByteStreamControllerCallPullIfNeeded (node:internal/webstreams/readablestream:3145:24)
    at readableByteStreamControllerPullSteps (node:internal/webstreams/readablestream:3254:3)
    at [kPull] (node:internal/webstreams/readablestream:1201:5)
    at readableStreamDefaultReaderRead (node:internal/webstreams/readablestream:2269:39)
    at nextSteps (node:internal/webstreams/readablestream:493:7)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async writeIterable (C:\Users\Angger\Desktop\project\dcjs-reproduce\node_modules\.pnpm\[email protected]\node_modules\undici\lib\dispatcher\client-h1.js:1437:22)

see code sample below to reproduce.
sample dcjs-reproduce.zip

Code sample

import * as undici from "undici";
import * as discordjs from "discord.js";
import path from "node:path";
import fs from "node:fs";

const BOT_TOKEN = ""; // replace with your bot token
const CHANNEL_ID = ""; // replace with your test channel

const discord = new discordjs.Client({
  intents: [discordjs.GatewayIntentBits.Guilds],
});

discord.on("ready", async (client) => {
  console.log("discord.js ready");
  const channel = await client.channels.fetch(CHANNEL_ID);

  if (!channel || !channel.isSendable()) {
    throw new Error("channel is null or not sendable");
  }

  try {
    // empty files
    const file = Buffer.alloc(0);
    // other API such a Stream, filename will also throw error
    // const file = path.resolve('test.jpg'); // use any test file
    // const file = fs.createReadStream(path.resolve('test.jpg'));

    console.log("channel.send({ files: [file] })");
    /**
     * if there a undici import this call will throw an error
     * TypeError: Cannot read properties of null (reading 'byteLength')
     * <see stack trace above>
     *
     * try to comment undici import above and rerun the script this call will working fine
     */
    await channel.send({
      files: [file],
      content: "message with file",
    });
    console.log("channel.send({ files: [file] }): sent");
  } catch (error) {
    console.log(error);
  }

  try {
    console.log('channel.send({ content: "ok" })');
    /**
     * this call is working fine no matter undici import is commented or not
     */
    await channel.send({
      content: "ok",
    });
    console.log('channel.send({ content: "ok" }): sent');
  } catch (error) {
    console.log(error);
  }
});

discord.login(BOT_TOKEN);

Versions

  • discord.js 14.20.0
  • undici 7.10.0
  • node 22.15.0

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds

I have tested this issue on a development release

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions