esbuild cannot resolve the Node.js module 'events'

Viewed 1383

One of my dependencies contains the following code

var EventEmitter = require('events').EventEmitter;

exports.EventEmitter = EventEmitter;
exports.mixin = mixin;

function mixin(Constructor) {
  for (var key in EventEmitter.prototype) {
    Constructor.prototype[key] = EventEmitter.prototype[key];
  }
}

I got the following error when trying to bundle everything into a file

$ npm run build


> gantt@0.0.0 build
> esbuild public/js/src/main.ts --bundle --minify --sourcemap --outfile=public/js/dest/bundled.js

 > node_modules/sharedb/lib/emitter.js:1:27: error: Could not resolve "events" (use "--platform=node" when building for node)
    1 │ var EventEmitter = require('events').EventEmitter;
      ╵                            ~~~~~~~~

1 error

I am targeting browsers, so it's impractical to have --platform=node. I discovered this issue when searching around, but the offending line is in a dependency, so I cannot simply edit its source to replace the CommonJS module import with an ES module import.

How can I point esbuild to the implementation of events?

0 Answers
Related