What could be causing my Discord bot to remain offline even when I run the command node main.js?

After successfully installing node.js, I proceeded to type the command 'npm init' in the command prompt and then installed discord.js. However, upon installation of discord.js, a 'node_modules' folder was not added inside the project, which I believe is causing the issue. When attempting to bring my bot online using the command 'node main.js', I encountered the following 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 ]
}

The contents of my package.json file are as follows:

{
  "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": "*"
  }

As for the content of main.js:

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

const client = new Discord.Client();

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

client.login('bot-token');

Answer №1

To fix the issue mentioned by derpirscher, simply delete the line

"discord.js": "*"
from your package.json file and then proceed to run npm install discord.js.

Your main.js code needs updating. Starting from version 13 onwards, you must include intents. Replace

const client = new Discord.Client();
with:

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

You can also choose different intents based on your requirements, as outlined in the documentation available here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The disappearing act of Redux state after being added to a nested array

When attempting to update my redux state, I am facing an issue where the state disappears. My approach involves checking for a parentId - if one exists, I insert the payload into the parent's children array. However, if no parentId is provided, I simp ...

Tips for editing bootstrap-vue table columns using non-Latin characters?

I need to create a table using the Cyrillic alphabet, but the keys of the object must be in the Latin alphabet within the program. Example : export default { data() { return { wmsFields: ['№', 'Наименование', ...

When attempting to submit a record at http://localhost:5173/, a 404 (Not Found) error was

Currently, I am attempting to retrieve username data and timeTaken from a form. My goal is to send this data to my server, create the User object, and store it in MongoDB Atlas. Unfortunately, I am encountering a 404 error that I am struggling to resolve. ...

Flutter: a feature that checks all checkboxes

(Resolved) Previously encountered an issue (now resolved) where selecting one checkbox resulted in all checkboxes being selected. View the image below for reference: https://i.sstatic.net/jh3fe.png (Unresolved) Seeking assistance from someone experienc ...

Transforming Child JSON Objects into Tables Using Xamarin Forms

I have a question that seems simple. Despite searching online, I couldn't find a solution. Here is the JSON data I received from an API: [ { "userId": "6ba8da0c-b307-40b6-b858-5fb1b880d473", "nomeCompleto& ...

Is it possible to apply styles to javascript elements without relying on img class? Additionally, how can I incorporate an onclick button while maintaining a fully functional navigation bar?

My current project involves creating an interactive collage where users can click around and have pictures pop up at the clicked location. The functionality works as intended, but now I'm facing issues with the navigation bar not being clickable. Addi ...

The attempt to load a JavaScript resource has resulted in an error: the file was not located, despite the fact that it is a

Recently, I came across a new challenge in my application. Whenever I navigate to specific pages, I notice an error message in the development console: inject.preload.js:373 GET blob:http://my-app-name.test/ba65127c-383e-45b7-8159-9b52ea288658 0 () Upon ...

Automatically populating fields in Laravel 8 after selecting a value from a dropdown menu

Having an issue once again.. If I choose the Banjarmasin-Jakarta route, the shipping cost field will be filled with 1.750.000 However, if I want to select "search shipping route" again, the shipping cost field will be filled with 0 Select "search shippi ...

Can Express POST / GET handlers accommodate the use of jQuery?

I created a simple Express server to retrieve data from an HTML form and make queries to OpenWeatherMap using that data: const { OpenWeatherAPI } = require("openweather-api-node"); const express = require("express"); const bodyParser = ...

The location of errors is not displayed in VueJS stack traces

My Current VueJS Setup Check out the Source Code here I am working on a project using VueJS with webpack. I have chosen not to use the vue-loader plugin or .vue files. My project structure resembles a typical Javascript webpack project where I import vu ...

Quotation marks in Strings are retained during the JSON response deserialization process

Utilizing reqwest, I am making a request to a Google API: let request_url = format!( "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=*\ &inputtype=textquery\ &fields=formatted_address,name,place_id, ...

Separating the logic of identical schemas and implementing multi-tenancy in Node.js using Mongoose

In the system I am developing, there are two key requirements: Each client needs to be completely isolated from one another Clients can have multiple subsidiaries which they should be able to switch between without needing to re-authenticate, while ensuri ...

Utilizing a Web Interface for Remote Monitoring of Windows Servers

I am in need of creating a webpage that will indicate whether a server is currently operational or not. These servers are all Windows based, with some running on 2008 and others on 2003. They are spread across different networks within various client locat ...

How to use jQuery to set a background image using CSS

I've been working on setting backgrounds dynamically with a jQuery script, but it seems like the .css function is not working as expected. Here's the code snippet: $(document).ready(function () { $(".VociMenuSportG").each(function () { ...

The <a> tag does not lead to a different webpage and cannot be clicked on

I have developed a web component that includes a method to generate a copyright string: '<p>Copyright © 2020 John Doe<a href="https://www.example.com">. Terms of Use</a></p>' After creating the string, I conver ...

Can you explain the exact meaning of XMLHttpRequest.XMLHttpRequest?

I find MDN's writing style to be confusing. On the MDN page about XMLHttpRequest, it states: XMLHttpRequest is an API ... Constructor XMLHttpRequest.XMLHttpRequest Properties XMLHttpRequest.onreadystatechange XMLHttpRequest.readyState XMLHttpReq ...

What is the best method for adding JSON objects in Logic Apps?

We are receiving a JSON message and want to include additional JSON data (a JSON object with various fields) in the original message. How can we insert the "GlossDef" JSON Object at the specified location below? { "glossary":{ "ti ...

Issue with Angular ngFor within a particular dialog window?

(respPIN and internalNotes are both of type InternalNotes[]) When the code in encounter.component.ts is set like this: this.ps.GetInternalNotes(resp.PersonID.toString()).subscribe(respPIN => { this.internalNotes = respPIN; }); An ERROR occurs: Err ...

Exploring the world of JSON communication through MVC Web API: A comprehensive guide

Similar cases to mine have been answered before, but my specific needs always seem to differ from others' problems. I am facing an issue where I'm sending JSON data from my HTML page to the MVC Web API, but unfortunately, the data I receive is a ...

Adding JSON to the body request in Jmeter

Here is my HTTP body request: { "FormTemplate": "[{\"_reference\":\"ID.ParentId1\",\"MetadataId\":111,\"fieldLabel\":\"ClientLable\"}]", "Type": 1, "Fields": [ { "MetadataFieldId" ...