Search code examples
javascriptnode.jswebpacknode-amqplib

Error: Can't resolve 'net' after adding the rabbitode package to my React app


Just added rabbitode to my React TypeScript app so I can read from RabbitMQ. The instructions are quite straightforward, however, I hit a number of issues after installation. My module doesn't compile at all. There were a number of issues related to my using webpack 5, as described here this answer. Most of these could be resolved with the provided answer (util, buffer and querystring-es3) but I'm getting no joy with stream-browserify. Also, there are a couple of errors that I can't track down at all:

ERROR in ./node_modules/rabbitode/node_modules/amqplib/lib/connect.js 155:11-33

Module not found: Error: Can't resolve 'net' in 'c:\myapp\node_modules\rabbitode\node_modules\amqplib\lib'
ERROR in ./node_modules/rabbitode/node_modules/amqplib/lib/connect.js 157:11-33

Module not found: Error: Can't resolve 'tls' in 'c:\myapp\node_modules\rabbitode\node_modules\amqplib\lib'

The lines the error is about are:

  if (protocol === 'amqp:') {
    sock = require('net').connect(sockopts, onConnect);
  }
  else if (protocol === 'amqps:') {
    sock = require('tls').connect(sockopts, onConnect);
  }
  else {
    throw new Error("Expected amqp: or amqps: as the protocol; got " + protocol);
  }

Thought this could be related to my node version as net and tls are available libraries according to the docs but the issue remains.

Any idea about how to fix it??

Also, the stream error is:

ERROR in ./node_modules/rabbitode/node_modules/amqplib/lib/connection.js 18:13-37

Module not found: Error: Can't resolve 'stream' in 'c:\myapp\node_modules\rabbitode\node_modules\amqplib\lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }

That I tried resolving by running npm install stream-browserify and adding stream: require.resolve("stream-browserify"), to the the fallback object in the webpack config. Why would this not work??

UPDATE

I think the problem is related to a couple of key facts:

  1. I bootstrapped my application with the create-react-app and therefore by webpack config was ignored
  2. rabbitode is relying on packages that were not meant for browser use.

I think the solution here is to look for a library that is more fit for purpose (or add an SseEmitter into my SpringBoot backend that reads from the queue, then read its contents in the client via an EventSource


Solution

  • I'm the author of the package, unfortunately it's not meant for use on frontend applications it's meant for node.js servers.

    If you did find a workaround to get it working on the frontend I'd love to hear it and I'll see if I can add native support to it :)

    Thanks Evan