next-runtime icon indicating copy to clipboard operation
next-runtime copied to clipboard

Does not work on with NextJS Edge runtime (dependency on body-parser -> fs not supported on Vercel Edge)

Open Winwardo opened this issue 2 years ago • 1 comments

Hi, I love this library, very nifty :)

This issue may be too early for you to consider, but next-runtime does not work with a NextJS page using the experimental-edge runtime. https://nextjs.org/docs/advanced-features/react-18/switchable-runtime#page-runtime-option

Specific error message when building:

Failed to compile.
--
18:18:36.899 |  
18:18:36.899 | ./node_modules/body-parser/node_modules/destroy/index.js:16:0
18:18:36.899 | Module not found: Can't resolve 'fs'
18:18:36.899 |  
18:18:36.899 | Import trace for requested module:
18:18:36.899 | ./node_modules/body-parser/lib/read.js
18:18:36.899 | ./node_modules/body-parser/lib/types/json.js
18:18:36.900 | ./node_modules/body-parser/index.js
18:18:36.900 | ./node_modules/next-runtime/runtime/body-parser.js
18:18:36.900 | ./node_modules/next-runtime/handle.js
18:18:36.900 | ./node_modules/next-runtime/index.js
18:18:36.900 | ./src/pages/admin/payments/attempt/[idempotencyKey].tsx

It looks like body-parser has a hard dependency on fs for some reason, and file-system activity is not available in Edge functions. You only use body-parser in one place, here: https://github.com/smeijer/next-runtime/blob/main/src/runtime/body-parser.ts#L104

Possible fixes:

  • Maybe updating body-parser from 1.19.0 to 1.20.0 (I don't think this will work)
  • Importing only json from 'body-parser (might not actually shake out the fs usage)
  • Find an alternative existing library (I couldn't find an obvious one, and I don't know enough about how you're using it to suggest one)
  • Write your own body parser (possibly very difficult?)
  • Add to documentation that you won't / can't support the edge runtime.

Thanks for all your work here

Winwardo avatar Aug 12 '22 17:08 Winwardo

Hi! Thanks for the report. I'd be happy to support the edge runtime. Finding a way to remove the fs dependency sounds like a good thing to me. We don't need it. I have no idea (without researching) why bodyparser depends on it.

smeijer avatar Aug 24 '22 17:08 smeijer