upstash/ratelimit-js

Provided Next.js middleware example does not seem to actually work with Next.js

DavidChouinard opened this issue · 2 comments

Copying the example provided here to our project, I get the following error:

Error: Module not found: Can't resolve 'stream'

Import trace for requested module:
./node_modules/isomorphic-fetch/fetch-npm-node.js
./node_modules/@upstash/redis/esm/platforms/nodejs.js
./pages/api/_middleware.ts


https://nextjs.org/docs/messages/module-not-found

You're using a Node.js module (stream) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

It seems that @upstash/redis depends on stream but it's not possible to use the stream module in a Next.js Edge function. A seperate Vercel middleware example seems to use a slimed down version of a Redis client that doesn't depend on stream , but alas I'm not sure how that would integrate with the rate limiting module.

Is there a way to use @upstash/ratelimit with Next.js Edge functions out of the box?

hey @DavidChouinard,
I have updated the example and it's working as expected now :)
see https://sdk-ratelimit-cygudeq4k-upstash.vercel.app/

Great thank you!!