Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

325
Vistas
ReferenceError: __dirname no está definido en el alcance del módulo ES

Estoy revisando este tutorial en discord.js. Cuando ejecuté el código tal como estaba escrito, recibí errores como SyntaxError: Cannot use import statement outside a module .

Entonces, agregué "type": "module" a package.json

Me las arreglé para ejecutar los primeros ejemplos. Ahora, estoy trabajando en una sección de código:

 import dotenv from "dotenv"; dotenv.config(); const token = process.env.DTOKEN; // const fs = require('node:fs'); import fs from 'node:fs'; // const path = require('node:path'); import path from 'node:path'; // 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); // 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);

Recibo: ReferenceError: __dirname is not defined in ES module scope

No veo mucho para continuar en SO. Preguntas como esta me devuelven al punto de partida.

¿Qué me estoy perdiendo?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Este artículo ayuda a resolver el problema: https://bobbyhadz.com/blog/javascript-dirname-is-not-defined-in-es-module-scope

Aquí está el código del artículo:

 import path from 'path'; import {fileURLToPath} from 'url'; const __filename = fileURLToPath(import.meta.url); // 👇️ "/home/john/Desktop/javascript" const __dirname = path.dirname(__filename); console.log('directory-name 👉️', __dirname); // 👇️ "/home/borislav/Desktop/javascript/dist/index.html" console.log(path.join(__dirname, '/dist', 'index.html'));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda