Project Setup
Initialize the project for Discord app development using TypeScript
Overview
This guide walks you through setting up a clean, scalable Node.js + TypeScript project for building a Discord application. The structure and conventions follow modern best practices and are suitable for long-term maintenance.
Initializing the Project
Start by creating a new Node.js project. This will generate a package.json file
to manage dependencies, scripts, and project metadata.
npm init -yAfter running this command, a default package.json will be created in the root directory.
Installing TypeScript
Install TypeScript as a development dependency:
npm install -D typescriptTypeScript adds static typing, improves editor support, and helps prevent runtime errors.
Creating a TypeScript Configuration
Initialize the TypeScript configuration file:
npx tsc --initThis command creates a tsconfig.json file, which controls how your TypeScript code
is compiled into JavaScript.
Recommended Project Structure
Below is a clean, scalable, and professional directory structure tailored for a TypeScript-based Discord bot.
Run the following PowerShell command from your project root:
mkdir src\commands\messageCommands\general `
src\commands\slashCommands\general `
src\configs `
src\constants\images `
src\events\client `
src\events\server `
src\handlers `
src\interfaces `
src\utilsThis will generate the entire folder structure correctly in one step.
Folder Purpose Overview
-
commands/
messageCommands/general– Prefix-based or message commandsslashCommands/general– Slash command implementations
-
configs/ – Bot configuration files (env loaders, settings, etc.)
-
constants/ – Static values and reusable constants
-
events/
client– Discord client events (ready, interactionCreate, etc.)server– Process-level or external service events
-
handlers/ – Command, event, and interaction loaders
-
interfaces/ – TypeScript interfaces and types
-
utils/ – Helper and utility functions
Next Recommended Steps
- Add an
index.tsentry file insidesrc/
Updating tsconfig.json
Open tsconfig.json and ensure the following configuration is set:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"strict": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"rootDir": "./src",
"sourceMap": true,
"declaration": true,
"declarationDir": "./types"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}These options provide a good balance between type safety, performance, and compatibility with the Discord.js ecosystem.
Adding Build Scripts
Update the scripts section in your package.json file:
{
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
}
}- build – Compiles TypeScript into JavaScript
- start – Runs the compiled application
Verifying the Setup
Create a basic entry file at src/index.ts:
console.log('Project setup complete')Compile and run the project:
npm run build
npm startIf the message appears in your terminal, the project setup is complete.
Last updated on