Error [ERR_REQUIRE_ESM]: require() of ES Module for importing chatgpt
04:09 05 Mar 2023

I want to require chatgpt package but it throws an error. I have already installed node fetch 2.6.6 or 2.6.1 but it did not work. I have added "type": "module" to my package.json file too and the bot didn't go online.

My code:

// <--- Discord Stuff --->

const Discord = require("discord.js");
const client = new Discord.Client(
    { intents: 32767 },
    { partials: ["MESSAGE", "CHANNEL", "REACTION", "GUILD_MEMBER"] }
);
module.exports = client;

// <--- Discord Stuff --->

// Other Stuff

const ChatGPTAPI = require('chatgpt');
const api = new ChatGPTAPI({
    apiKey: process.env.OPENAI_API_KEY
})
  
require("dotenv").config();

// Other Stuff

client.on("messageCreate", async (message) => {

    if (message.channel.id == '1063790255880286269' && !message.author.bot) {
        const res = await api.sendMessage(message.content)
        message.channel.send({ content: `<@${message.author.id}> ${res.text}` })
    } else return;
})

process.on("unhandledRejection", (reason, p) => {
    console.log(" [antiCrash] :: Unhandled Rejection/Catch");
    console.log(reason, p);
});
process.on("uncaughtException", (err, origin) => {
    console.log(" [antiCrash] :: Uncaught Exception/Catch");
    console.log(err, origin);
});
process.on("uncaughtExceptionMonitor", (err, origin) => {
    console.log(" [antiCrash] :: Uncaught Exception/Catch (MONITOR)");
    console.log(err, origin);
});

client.login(process.env.DISCORD_TOKEN);
javascript node.js discord.js