Search code examples
javascriptjsondiscorddiscord.js

Why doesn't my discord bot get online when I type node main.js?


so I first installed node.js then typed npm init to command prompt then installed discord.js(When installed discord.js it didnt add a node_modules folder inside the project and I think thats where the problem originates from). But when I try to get my bot online(with node main.js command) I see this error message: Error:

Cannot find module 'discord.js'
Require stack:
- C:\Users\Cookie\Desktop\bot\main.js
←[90m    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:778:27)←[39m
←[90m    at Module.require (node:internal/modules/cjs/loader:1005:19)←[39m
←[90m    at require (node:internal/modules/cjs/helpers:102:18)←[39m
    at Object.<anonymous> (C:\Users\Cookie\Desktop\bot\main.js:1:17)
←[90m    at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: [ ←[32m'C:\\Users\\Cookie\\Desktop\\bot\\main.js'←[39m ]
}

here is my package.json code:

{
  "name": "bot",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Cookie482",
  "license": "ISC",
  "dependencies": {
    "discord.js": "*"
  }

and main.js:

const Discord = require('discord.js');

const client = new Discord.Client();

client.once('ready', () => {
    console.log('bot is online');
});

client.login('bot-token');

Solution

  • As derpirscher said, remove the line "discord.js": "*" from package.json and run npm install discord.js.

    Your main.js code is outdated. From v13 onwards you need to specify intents. Replace const client = new Discord.Client(); with

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

    or some other intents as specified in the documentation here