How to make Typescript recognize type files

Hi, I am a beginner developing in Typescript. I am trying to create a bot that can use commands using client.commands = new Collection(); based on the guide. https://discordjs.guide/creating-your-bot/command-handling.html#loading-command-files Main file is:
import {Message, Client, Collection, Events, GatewayIntentBits, TextChannel,
} from "discord.js"
import "./@types/client.d.ts"

client.commands = new Collection()
import {Message, Client, Collection, Events, GatewayIntentBits, TextChannel,
} from "discord.js"
import "./@types/client.d.ts"

client.commands = new Collection()
I am getting a type error, so I have created a type definition file at /src/@types/client.d.ts. The contents of the file are as follows:
import { Collection } from 'discord.js';

declare module "discord.js" {
export interface Client {
commands: Collection<unknown, any>
}
}
import { Collection } from 'discord.js';

declare module "discord.js" {
export interface Client {
commands: Collection<unknown, any>
}
}
However, when I run ts-node --files src/main.ts in vscode, I get the following error:
TypeError: Unable to require file: src/@types/client.d.ts
This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or other executable extension with loader attached before `ts-node` available.
TypeError: Unable to require file: src/@types/client.d.ts
This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or other executable extension with loader attached before `ts-node` available.
It may be a problem with tsconfig.json or ts-node, please let me know how to solve it. Sorry if this question has already been asked. npm -v
v16.16.0
v16.16.0
npm list discord.js
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
2 Replies
d.js toolkit
d.js toolkit2y ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
keppy
keppyOP2y ago
thx replying, but it does not run with error... (in only strict mode?)
$ npm run test
Debugger attached.

> ts-node --files src/main.ts

Debugger attached.
/home/keppy/bombies/node_modules/ts-node/src/index.ts:1085
throw new TypeError(
^
TypeError: Unable to require file: src/@types/client.d.ts
This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or other executable extension with loader attached before `ts-node` available.
at getOutput (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1085:17)
at Object.compile (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1433:41)
at Module.m._compile (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1617:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Object.require.extensions.<computed> [as .ts] (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1621:12)
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)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/keppy/bombies/src/main.ts:9:1)
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...
$
$ npm run test
Debugger attached.

> ts-node --files src/main.ts

Debugger attached.
/home/keppy/bombies/node_modules/ts-node/src/index.ts:1085
throw new TypeError(
^
TypeError: Unable to require file: src/@types/client.d.ts
This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or other executable extension with loader attached before `ts-node` available.
at getOutput (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1085:17)
at Object.compile (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1433:41)
at Module.m._compile (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1617:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Object.require.extensions.<computed> [as .ts] (/home/keppy/bombies/node_modules/ts-node/src/index.ts:1621:12)
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)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/keppy/bombies/src/main.ts:9:1)
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...
$
the bot stopped.
import {
Message,
Client,
Collection,
Events,
GatewayIntentBits,
TextChannel,
} from "discord.js"
import "./@types/client.d.ts"

import fs from "node:fs"
import path from "node:path"
import dotenv from "dotenv"

import { logMessage } from "./logger"
import { Achievement } from "./achievement"
import { JsonDB } from "./bombiesSqlite3"

dotenv.config()

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
})

client.once("ready", () => {
console.log("Ready!")
if (client.user) {
console.log(client.user.tag)
}
new JsonDB("roles", "achievements").createTable()
const achivement = new Achievement("1234567890").overwrite(
"",
[],
false,
null,
""
)
new Achievement("1234567890").get()
})

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
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command)
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
)
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

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







client.on("messageCreate", async (message: Message) => {
if (message.author.bot) return

// Message Logs
if (message.channel instanceof TextChannel) {
console.log(new logMessage(message, message.channel).saveOnFile())
}

// Message Response
if (message.content.startsWith("!ping")) {
message.channel.send("Pong!")
}
})

client.login(process.env.TOKEN)
import {
Message,
Client,
Collection,
Events,
GatewayIntentBits,
TextChannel,
} from "discord.js"
import "./@types/client.d.ts"

import fs from "node:fs"
import path from "node:path"
import dotenv from "dotenv"

import { logMessage } from "./logger"
import { Achievement } from "./achievement"
import { JsonDB } from "./bombiesSqlite3"

dotenv.config()

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
})

client.once("ready", () => {
console.log("Ready!")
if (client.user) {
console.log(client.user.tag)
}
new JsonDB("roles", "achievements").createTable()
const achivement = new Achievement("1234567890").overwrite(
"",
[],
false,
null,
""
)
new Achievement("1234567890").get()
})

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
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command)
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
)
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

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







client.on("messageCreate", async (message: Message) => {
if (message.author.bot) return

// Message Logs
if (message.channel instanceof TextChannel) {
console.log(new logMessage(message, message.channel).saveOnFile())
}

// Message Response
if (message.content.startsWith("!ping")) {
message.channel.send("Pong!")
}
})

client.login(process.env.TOKEN)
Wow, It seems that there was some fundamental misunderstanding! thx so much
Want results from more Discord servers?
Add your server