Home > Net >  discord.js typescript error - missing index.ts when executing 'ts-node index.ts'
discord.js typescript error - missing index.ts when executing 'ts-node index.ts'

Time:01-30

I am creating a discord bot with discord.js and typescript but when I try to turn the bot on, using 'ts-node index.ts' this is the error I am given:

Error: Cannot find module 'node:events'
Require stack:
- /home/ether0p12348/RainCrown/node_modules/discord.js/src/client/BaseClient.js
- /home/ether0p12348/RainCrown/node_modules/discord.js/src/index.js
- /home/ether0p12348/RainCrown/index.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/lib/node_modules/ts-node/node_modules/@cspotcode/source-map-support/source-map-support.js:679:30)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/ether0p12348/RainCrown/node_modules/discord.js/src/client/BaseClient.js:3:22)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/ether0p12348/RainCrown/node_modules/discord.js/src/client/BaseClient.js',
    '/home/ether0p12348/RainCrown/node_modules/discord.js/src/index.js',
    '/home/ether0p12348/RainCrown/index.ts'
  ]
}

This is what I have in index.ts in the bot's root directory

const Discord = require('discord.js')

import DiscordJS, {Intents} from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
    ]
})

client.on('ready', () => {
    console.log('RainCrown is ready!')
})

client.on('messageCreate', (message) => {
    if (message.content === 'ping') {
        message.reply({
            content: 'pong',
        })
    }
})

client.login(process.env.TOKEN)

all I have in ".env" is my token (edited)

my 'ls' output: index.ts node_modules package.json package-lock.json tsconfig.json

If anyone could guide me on what steps I should take to resolve this error, that would be greatly appreciated.

ps: I am using a raspberry pi with the ubuntu OS to host this bot

CodePudding user response:

You are propably not using the right node version. Try upgrading it. The newer discordjs needs atleasr v16.6. Check out this post error when requiring discordjs - cannot find module 'node:events'. I faced the same issue on 32 bit machine where i have problems installing node16, so I used the older discordjs version.

CodePudding user response:

As Borecjeborec1 said, I just had to update my node.js to 16.6. This is the document that helped my achieve this:

https://computingforgeeks.com/how-to-install-node-js-on-ubuntu-debian/

The solution that worked for me was installing 'nvm' and installing v16 from there (solution 2 on document).

  •  Tags:  
  • Related