Can't use Discordjs in Nextjs server actions
Answered
Giant panda posted this in #help-forum
Giant pandaOP
I have created a server action like this:
/// /lib/actions.ts
"use server";
import { Client } from "discord.js";
export async function create(formData: FormData) {
const discordClient = new Client({
intents: ["GuildMessages", "Guilds"],
});
}
And then I have a simple form in a page that uses this action:
<form action={create} >
/// some elements
</form>
However, everytime I build, I get this error:
Module not found: Can't resolve 'zlib-sync'
I tried commenting each line, and when I comment Discord Client, the error goes away.
I think webpack is trying to include discordjs in the client-side (which it shouldn't).
How can I solve this issue?
/// /lib/actions.ts
"use server";
import { Client } from "discord.js";
export async function create(formData: FormData) {
const discordClient = new Client({
intents: ["GuildMessages", "Guilds"],
});
}
And then I have a simple form in a page that uses this action:
<form action={create} >
/// some elements
</form>
However, everytime I build, I get this error:
Module not found: Can't resolve 'zlib-sync'
I tried commenting each line, and when I comment Discord Client, the error goes away.
I think webpack is trying to include discordjs in the client-side (which it shouldn't).
How can I solve this issue?
Answered by Giant panda
I actually found the solution here ( https://www.armannotes.com/2024/02/12/zlib-error-nextjs-server-actions/ ) and solved it.
2 Replies
Giant pandaOP
I actually found the solution here ( https://www.armannotes.com/2024/02/12/zlib-error-nextjs-server-actions/ ) and solved it.
Answer
@Giant panda I actually found the solution here ( https://www.armannotes.com/2024/02/12/zlib-error-nextjs-server-actions/ ) and solved it.
the real question is what are you doing with discord.js and why aren't you using @discordjs/rest or just doing the requests directly (you don't need the whole bot for an action surly)