'./index.js is recognized as a module
./commands/hello.js:
const discord = require("discord.js")
const client = require("./index.js").client
const { prefix } = require("./config/config.json")
const { hello } = "./config/config.js"
//modules
module.exports = () => {
//onClient
client.on("messageCreate", (message) => {
if (!message.content.startsWith(`${prefix}hello`)) return
//creating the embed
const embed = new discord.MessageEmbed()
.setTitle("test")
.setAuthor("Sanke")
.addField("Test", "this is a test")
.setDescription("This is a test commmand")
//message send
message.channel.send({
content:"",
embeds: [embed],
files: []
})
})
}
index.js:
const { Client,Intents } = require("discord.js")
const { token } = require("./config/config.json")
const fs = require('fs')
//Client init
const client = new Client({ intents: [Intents.FLAGS.GUILDS]})
exports.client = client
client.once('ready', () =>{
console.log("First time seeing ya face on discord :)")
client.user.setActivity("First time here :D")
})
//comand Handler
const command = require("./commands/hello")
command()
client.login(token)
Problem: -in ./commands/hello.js is recognized as a module,not a target file. I don't know what to do...I tried to make the command handler with the const command = require("./commands(commands directory)/hello(the command file)") -Error: node:internal/modules/cjs/loader:936 throw err; ^
Error: Cannot find module './index.js'
Require stack:
- /Users/sanki/Desktop/Sanke_<:>/proiecte/theBot/commands/hello.js
- /Users/sanki/Desktop/Sanke_<:>/proiecte/theBot/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/sanki/Desktop/Sanke_<:>/proiecte/theBot/commands/hello.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/sanki/Desktop/Sanke_<:>/proiecte/theBot/commands/hello.js',
'/Users/sanki/Desktop/Sanke_<:>/proiecte/theBot/index.js'
]
}
Solution 1:[1]
It looks from the code you shared that you're trying to require ./index.js inside commands/hello.js. Shouldn't that be ../index.js?
The next problem you're likely to encounter is a circular dependency - each file requires the other.
Can you remove the require of index.js in hello.js and pass the client to the hello command directly? E.g.
index.js
const { Client, Intents } = require("discord.js")
const { token } = require("./config/config.json")
const fs = require('fs')
//Client init
const client = new Client({ intents: [Intents.FLAGS.GUILDS]})
exports.client = client
client.once('ready', () =>{
console.log("First time seeing ya face on discord :)")
client.user.setActivity("First time here :D")
})
//comand Handler
const command = require("./commands/hello")
// CHANGE >> PASS CLIENT TO COMMAND
command(client)
client.login(token)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andru |
