grammY: "exports is not defined" With Next.js edge runtime functions
Hi there,
I’m using Next.js api, with an edge runtime. When I set up an API handler like this:
// pages/api/bot.ts
import { Bot } from "grammy/web";
import { NextResponse } from "next/server";
export const runtime = "edge";
export default async function handler() {
new Bot("<token>");
return new NextResponse("Success", { status: 200 });
}
When accessing the route, I get this server error:
ReferenceError: exports is not defined
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
webpack-internal:///(middleware)/./node_modules/grammy/out/web.mjs (4)
eval
webpack-internal:///(middleware)/./node_modules/grammy/out/web.mjs (4:23)
This is related how cjs modules are exported and how it works with edge. Maybe it has to do with swc bundling.
Repo with the error reproduction: https://github.com/va3y/grammy-next-edge
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 19 (10 by maintainers)
Hehe alright, good luck with the other stuff 😄
This issue is currently tracked at https://github.com/vercel/next.js/issues/51313, as well. I think that the people there know a lot more about how their runtime works. @va3y, you’ll need to provide a complete reproduction example for them. I will close this issue in favour of the other one.
If you end up finding out that grammY is broken and your setup is correct, and we thus have to adjust something in grammY, please open a new issue. The comment at https://github.com/vercel/next.js/issues/51313#issuecomment-1594888767 seems to imply that the web build grammY is correct, so I don’t yet see what we would have to do here.
This example does not work for me with the latest version of grammy 1.22.4. It does work with your branch
npm:@ponomarevlad/grammy@1.19.2-next-edge
.It’s an ES module. You can just look at the code inside. It uses
export { Bot as Bot }
syntax.