amqp-node / amqplib

AMQP 0-9-1 library and client for Node.JS
https://amqp-node.github.io/amqplib/
Other
3.67k stars 474 forks source link

Module not found: Can't resolve 'querystring' #754

Closed benyaminl closed 7 months ago

benyaminl commented 7 months ago

Hello, I'm trying to use this client on nextjs, but I got error Module not found: Can't resolve 'querystring',

I'm using node 20 and nextjs 14

Error ⨯ ./node_modules/amqplib/lib/connect.js:10:0 Module not found: Can't resolve 'querystring'

image

I do check that Node 20 has querystring https://nodejs.org/docs/latest-v20.x/api/querystring.html

But why I got this error, it's what make me confused.

if is there any pointer regarding this, I would be thankful.

benyaminl commented 7 months ago

Ah... the problem is the lib/querystring.js isn't initiated on edge runtime (https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation#importing-runtime-specific-code)

https://github.com/vercel/next.js/discussions/50198

:/

So I need to manually use the import() and doing by this way..

export async function register()
{
    if (process.env.NEXT_RUNTIME === 'nodejs') {
        await import("./lib/rabbitmq").then(async (d) => {
            await d.PubSub();
        });
    }
}
cressie176 commented 7 months ago

@benyaminl glad you got it sorted and thank you for posting the solution