[Solved] Error: Cannot find module ‘node:events’

Total
15
Shares

If you are trying to use the Discord.js to develop any custom application and if you are not using latest version of Node.js you will get an Error: Cannot find module ‘node:events’

In this tutorial, we will look at what is Error: Cannot find module ‘node:events’ and how to fix the issue with an example.

What is Error: Cannot find module ‘node:events’?

Discor.JS is mainly used to create the custom bots in Discord. Discord.js is a powerful Node.js  module that allows you to easily interact with the Discord API .

We can install the Discord.js using the npm/yarn as shown below

npm install discord.js
yarn add discord.js
pnpm add discord.js

Once installed, let’s say we create a simple bot using discord.js

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

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

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
  }
});

client.login('token');

Output

internal/modules/cjs/loader.js:892
  throw err;
  ^

Error: Cannot find module 'node:events'

When we run the code it will fail and we will get an Error: Cannot find module ‘node:events’

How to fix Error: Cannot find module ‘node:events’ ?

The discord.js is a node module and it is developed using the latest version of node.js. So if you have the older version of the node.js and try to build a discord bot you will get this error.

We can fix the Error: Cannot find module ‘node:events’ by upgrading the node.js to version 16.6.0 or above.

We can upgrade the node.js to latest version by running the below command on windows.

npm cache clean
npm update -g

For installing specific version or if you want to upgrade node.js in other environments such as linux checkout How to upgrade Node.js in different environments

Conclusion

The Error: Cannot find module ‘node:events’ occurs if you are not using the latest version of node.js 16.6.0 or above and developing the discord bots.

We can fix the issue by upgrading the Node.js to 16.6.0 version or above using the npm update -g command and running the application again.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like