I dont know what this error means

the error:
E:\Discord bots\Custom bot order\events\connect.js:2
const chalk = require("chalk");
^

Error [ERR_REQUIRE_ESM]: require() of ES Module E:\Discord bots\Custom bot order\node_modules\chalk\source\index.js from E:\Discord bots\Custom bot order\events\connect.js not supported.
Instead change the require of index.js in E:\Discord bots\Custom bot order\events\connect.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (E:\Discord bots\Custom bot order\events\connect.js:2:15)
at Object.<anonymous> (E:\Discord bots\Custom bot order\index.js:35:17) {
code: 'ERR_REQUIRE_ESM'
}

Node.js v18.18.0
E:\Discord bots\Custom bot order\events\connect.js:2
const chalk = require("chalk");
^

Error [ERR_REQUIRE_ESM]: require() of ES Module E:\Discord bots\Custom bot order\node_modules\chalk\source\index.js from E:\Discord bots\Custom bot order\events\connect.js not supported.
Instead change the require of index.js in E:\Discord bots\Custom bot order\events\connect.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (E:\Discord bots\Custom bot order\events\connect.js:2:15)
at Object.<anonymous> (E:\Discord bots\Custom bot order\index.js:35:17) {
code: 'ERR_REQUIRE_ESM'
}

Node.js v18.18.0
connect.js code:
11 Replies
d.js toolkit
d.js toolkit9mo 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
NNKtv28
NNKtv289mo ago
const mongoose = require("mongoose");
const chalk = require("chalk");

async function connect() {
mongoose.set("strictQuery", false);
try {
console.log(
chalk.blue(chalk.bold(`Database`)),
chalk.white(`>>`),
chalk.red(`MongoDB`),
chalk.green(`is connecting...`)
);
await mongoose.connect(process.env.mongoConnectionStrin, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (err) {
console.log(
chalk.red(`[ERROR]`),
chalk.white(`>>`),
chalk.red(`MongoDB`),
chalk.white(`>>`),
chalk.red(`Failed to connect to MongoDB!`),
chalk.white(`>>`),
chalk.red(`Error: ${err}`)
);
console.log(chalk.red("Exiting..."));
process.exit(1);
}

mongoose.connection.once("open", () => {
console.log(
chalk.blue(chalk.bold(`Database`)),
chalk.white(`>>`),
chalk.red(`MongoDB`),
chalk.green(`is ready!`)
);
});

mongoose.connection.on("error", (err) => {
console.log(
chalk.red(`[ERROR]`),
chalk.white(`>>`),
chalk.red(`Database`),
chalk.white(`>>`),
chalk.red(`Failed to connect to MongoDB!`),
chalk.white(`>>`),
chalk.red(`Error: ${err}`)
);
console.log(chalk.red("Exiting..."));
process.exit(1);
});
}
const mongoose = require("mongoose");
const chalk = require("chalk");

async function connect() {
mongoose.set("strictQuery", false);
try {
console.log(
chalk.blue(chalk.bold(`Database`)),
chalk.white(`>>`),
chalk.red(`MongoDB`),
chalk.green(`is connecting...`)
);
await mongoose.connect(process.env.mongoConnectionStrin, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (err) {
console.log(
chalk.red(`[ERROR]`),
chalk.white(`>>`),
chalk.red(`MongoDB`),
chalk.white(`>>`),
chalk.red(`Failed to connect to MongoDB!`),
chalk.white(`>>`),
chalk.red(`Error: ${err}`)
);
console.log(chalk.red("Exiting..."));
process.exit(1);
}

mongoose.connection.once("open", () => {
console.log(
chalk.blue(chalk.bold(`Database`)),
chalk.white(`>>`),
chalk.red(`MongoDB`),
chalk.green(`is ready!`)
);
});

mongoose.connection.on("error", (err) => {
console.log(
chalk.red(`[ERROR]`),
chalk.white(`>>`),
chalk.red(`Database`),
chalk.white(`>>`),
chalk.red(`Failed to connect to MongoDB!`),
chalk.white(`>>`),
chalk.red(`Error: ${err}`)
);
console.log(chalk.red("Exiting..."));
process.exit(1);
});
}
idk if its related to discord.js but if its not pls dont close the channel and help me solve the issue
Danial
Danial9mo ago
It's not discord.js-related, but as the error says, require() isn't supported as newer versions of chalk require ESM, so either install an older version of chalk or use ESM, for more help, please ask in #other-js-ts / #useful-servers
NNKtv28
NNKtv289mo ago
its the latest version of chalk and whats ESM its the first time i hear about it
Danial
Danial9mo ago
In CJS (CommonJS), what you're using, you do const ... = require("...") but in ESM (EcmaScript Modules), you do import ... from "...", this is just JavaScript now and not discord.js-related
NNKtv28
NNKtv289mo ago
ohh alright so i should do import chalk from "chalk" then? nvm doesnt work cuz i cant use import outside a statement
Danial
Danial9mo ago
You'd need to change that everywhere in your code and also set the type to module in package.json, and again, this isn't discord.js-related
kin.ts
kin.ts9mo ago
I think there is a method that can be used to import the ESM module in CJS, import("module-name"), This returns a promise, so you need to resolve it first
const chalk = import("chalk").then(ctx);
const chalk = import("chalk").then(ctx);
I'm not sure if this is work or not, haven't used CJS in a while
NNKtv28
NNKtv289mo ago
yes ik it aint discord.js related T_T
Danial
Danial9mo ago
Then please do what I said
NNKtv28
NNKtv289mo ago
alr sorry