4

I have just started Discord.JS on Replit. It followed FreeCodeCamp's Tutorial on https://www.freecodecamp.org/news/create-a-discord-bot-with-javascript-nodejs/

But when I runned the code it telled me to run :

const Discord = require("discord.js")
const client = new Discord.Client()

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`)
})

client.on("message", msg => {
  if (msg.content === "ping") {
    msg.reply("pong");
  }
})

client.login(process.env.TOKEN)

It tells this error :

Error: Cannot find module 'node:events'
Require stack:
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/Omega-Flowey-JS-Test/node_modules/discor
Error: Cannot find module 'node:events'
Require stack:
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/index.js
- /home/runner/Omega-Flowey-JS-Test/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    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/runner/Omega-Flowey-JS-Test/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)
    at Module.require (internal/modules/cjs/loader.js:887:19

Can someone tell me whats error? I'm very new at Discord.JS so I don't know anything that is error.

MenIndo
  • 53
  • 1
  • 6
  • Did you get an errors when you ran `npm install` ? – Alan Oct 06 '21 at 02:48
  • I don't know, when I started it, it just instantly gives that error. Like how the program just doesn't want to try to run and just gives an error – MenIndo Oct 06 '21 at 08:50

1 Answers1

6

Discord.js v13 requires Node.js v16.6. Replit only directly supports Node.js v12.16.1 (at the time of writing).

When using an older version of Node.js, you may see errors such as

However, you can install a newer Node.js binary using the node npm package.

Here's a minimal setup:

package.json

{
  "name": "node.js-v16-on-replit-demo",
  "version": "0.0.1",
  "dependencies": {
    "discord.js": "^13.2.0",
    "node": "^16.10.0"
  }
}

.replit

run = "npx node ."

index.js

const Discord = require("discord.js")

// your code...

You can try this out with this demo repl.

If you like, you can define a start npm script:

package.json

{
  // ...
  "scripts": {
    "start": "node ."
  }
}

.replit

run = 'npm start'

There are also plenty of resources online about Node.js v16 on Replit.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59