How to Fix error: cannot find module ‘node:events’ in Node.js

Error: cannot find module ‘node:events’ occur in Node.js when you try to “use a built-in module using the node: prefix, which was introduced in Node.js version 14.x for ES modules.”

There are two main reasons for the error.

  1. When running an older version of Node.js that doesn’t support the “node:” prefix.
  2. When you have misconfigured the ES modules in your project.

How to fix the error?

Here are three solutions to fix the error.

  1. Upgrade your Node.js version to the latest version.
  2. Use Without “node:” Prefix if you are using an older version.
  3. Ensure Proper ES Module Configuration.

Solution 1: Upgrade your Node.js version to the latest version

The easiest way to fix the cannot find module ‘node:events’ error is by upgrading your system’s Node.js to the latest version.

Solution 2: Use Without “node:” Prefix if you are using an older version.

If you are on an older version of Node.js and can’t update for some reason, or if you just want to avoid the prefix, you can simply require the “events” module without the “node:” prefix:

import { EventEmitter } from 'events';

or using CommonJS:

const { EventEmitter } = require('events');

Solution 3: Ensure Proper ES Module Configuration

If you are using ES modules (with import/export syntax), ensure that you have set the “type” field in your package.json to “module”.

{
  "type": "module"
}

This configuration lets Node.js know you are using ES modules in your project.

I hope these solutions will fix your error.

Related posts

nodemon clean exit – waiting for changes before restart

Error: listen EADDRINUSE: address already in use in Node.js

ReferenceError: primordials is not defined error in Node.js