Home > Software design >  Getting weird errors when running node index.js w/npm
Getting weird errors when running node index.js w/npm

Time:04-04

Okay so I was trying to make a discord bot following a tutorial when I noticed some weird errors.

This is my code:

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

const client = new Discord.Client();

const prefix = '-';

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

client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot ) return;

    const args = message.content.slice(prefix.lenght).split(/  /);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        message.channel.send('pong!');
    }
});

client.login(YOUR_TOKEN);

And these are the errors:

Debugger attached.
Waiting for the debugger to disconnect...
internal/modules/cjs/loader.js:818
  throw err;
  ^

Error: Cannot find module 'node:events'
Require stack:
- /media/oli/MID/Projects/JS/Bot/node_modules/discord.js/src/client/BaseClient.js
- /media/oli/MID/Projects/JS/Bot/node_modules/discord.js/src/index.js
- /media/oli/MID/Projects/JS/Bot/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> (/media/oli/MID/Projects/JS/Bot/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) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/media/oli/MID/Projects/JS/Bot/node_modules/discord.js/src/client/BaseClient.js',
    '/media/oli/MID/Projects/JS/Bot/node_modules/discord.js/src/index.js',
    '/media/oli/MID/Projects/JS/Bot/index.js'
  ]
}
              

May I remind you that running npm install on those packages didn't work and just returned a lot of npm errors. I don't know what to do Edit:

It's still saying that even when I updated node.js

CodePudding user response:

I think the issue is happening because you are using nodejs less than version 16.6.0. discord.js requires node.js version more than or equal to 16.6.0. Try running node -v command in your terminal and that might be less than 16.6.0 then download and install that version of node.

Also add this in your package.json

"engines": {
  "node": ">=16.6.0"
}

Also, in your code, you misspelled length.

enter image description here

CodePudding user response:

in order to use discord.js. So You just need to update nodejs to v16.6. And Your error will be solved.

  • Related