upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-vercel-kv.vercel.app
MIT License
1.65k stars 32 forks source link

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

Closed DavidChouinard closed 2 years ago

DavidChouinard commented 2 years ago

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?

chronark commented 2 years ago

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

DavidChouinard commented 2 years ago

Great thank you!!