0

I am following a tutorial on Discord.js. I have had to change require to import for node:fs and node:path.

On line 34, however: (const command = require(filePath);) there is another require statement that's throwing the same error about E5. My understanding from articles like this is import is always run at the beginning of the file. So, how do I deal with this error? ReferenceError: require is not defined in ES module scope, you can use import instead

I would also love to understand, why all the tutorials I see use require, but I always get an error and have to convert to import.

Here's my code:

import dotenv  from "dotenv";
dotenv.config();
const token = process.env.DTOKEN;

// const fs = require('node:fs');
import fs from 'node:fs';


// https://bobbyhadz.com/blog/javascript-dirname-is-not-defined-in-es-module-scope
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
console.log(import.meta.url);

// const path = require('node:path');
import path from 'node:path';
// import path from 'path'
const __dirname = path.dirname(__filename);


// Require the necessary discord.js classes
import { Client, Collection, Intents } from "discord.js";
// const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));


for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);  // *** here's the line with the error ***
    // Set a new item in the Collection
    // With the key as the command name and the value as the exported module
    client.commands.set(command.data.name, command);
}

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});


client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const { commandName } = interaction;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

// Login to Discord with your client's token
client.login(token);
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • `require()` is the older CommonJS import mechanism, while `import` is the newer ES6 module syntax. Node had modules before the language standardized. – Pointy Jun 01 '22 at 14:01
  • @Pointy Thank you! So, is there some systematic way for me to deal with this? – DBWeinstein Jun 01 '22 at 14:02
  • You can use a version of `import` that looks like a function call, `import("something")`, in an ES6 module (where everything is usually `import` and `export`), in Node. That doesn't work in browsers however (usually). – Pointy Jun 01 '22 at 14:07
  • 3
    Replace each of these `require`s by `await import`. This works in `async` functions or on the top level. – Sebastian Simon Jun 01 '22 at 14:08
  • @SebastianSimon your answer is what worked. Doyou want to provide it as an answer? – DBWeinstein Jun 02 '22 at 14:36
  • @DBWeinstein Sorry, I’ve been really busy the last few weeks, but the `await import` approach is also mentioned in some of the answers here: [Using Node.js require vs. ES6 import/export](https://stackoverflow.com/q/31354559/4642212). – Sebastian Simon Jul 05 '22 at 21:00

1 Answers1

0

Making a variable receive an import you will have to deal with the Promises that will come, for me it was a bit of work, so I redid the importations with require, I also recommend it.

To write imports with require just go to your package.json or package-lock.json and enter

    "type": "commonjs",
EMERALD
  • 1
  • 2