Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
¿Cómo reemplazo ```require()``` en este código?

Estoy siguiendo un tutorial sobre Discord.js . Tuve que cambiar require para import para node:fs y node:path .

Sin embargo, en la línea 34: ( const command = require(filePath); ) hay otra instrucción require que arroja el mismo error sobre E5. Mi entendimiento de artículos como este es que la import siempre se ejecuta al principio del archivo. Entonces, ¿cómo trato este error? ReferenceError: require is not defined in ES module scope, you can use import instead

También me encantaría entender por qué todos los tutoriales que veo usan require , pero siempre recibo un error y tengo que convertir a import .

Aquí está mi código:

 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);
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda