Cloudflare Pages build fails when a D1 binding is ...
# pages-help
s
12:31:37.893 ✨ Upload complete! 12:31:39.701 Success: Assets published! 12:31:41.298 Error: Failed to publish your Function. Got error: Uncaught Error: No such module "opt/buildhome/repo/.vercel/output/static/_worker.js/index.js". imported from "bundledWorker-0.3312669898700358.mjs" A clean Nextjs install will run and successfully build, but I found that when I add a binding to my D1, even though I have no functions folder and not using it, it fails. In my main project I am working on, it relies on D1 for a blog, and that one also fails. Found that when any binding is enabled for D1, build will always fail. Any help on how to remedy this or is this a bug?
w
I know there was an issue with next-on-pages and D1 recently, any idea if it's fixed @Better James ?
b
I believe Greg fixed it in wrangler a few days ago - Dario confirmed it was fixed for him a couple days ago
w
So wrangler beta branch probably fixes it
b
I think 3.0 has it
w
But this is Pages CI so can't just switch to beta... fun
s
Using git and the dashboard for builds, on v1 of next on pages, so I guess I gotta wait?
b
Yeah looking at the release notes it was included in wrangler@3
s
Alright, haha I was losing my mind, testing if it was v2 of the build system for why it wasnt working. I can just disable my binding for now, and wait til whenever an update comes out for the dashboard builds
b
You could try deploying through wrangler@3 if that's a feasible solution for you. It's possible that it could be an unrelated issue, because IIRC the fix that Greg did was, well, he described it as very hacky
s
I haven't used wrangler since the first version of 2, like months ago, but i will give it a try again
Thank for the quick replies, I appreciate it, on a Saturday, no less. 🙂
Just wanted to leave an update, was able to use C3 to deploy a new Nextjs app, and functions do work, the issue is you have to choose between the pages/api folder or functions, both will not work together. Currently I was testing this with a pages/api/hello.js file, and a functions/api/v0/blog.js file.
Is it possible to call the D1 context inside of the nextjs api folders?
b
Using C3 at the moment will give you an old version of next.js and by default use an old version of next-on-pages that i would consider unacceptable to deploy an app with now that v1 is out. (PR is open to update this to an acceptable standard)
The only reason you will have success deploying one created by C3 is because the old version of next-on-pages it defaults to does not use the new approach of building the worker file that we adopted to enable deploying apps that exceed the cpu startup time limit.
I'm also not really sure what you mean by functions. You should use normal server functionality/nextjs api routes, not pages functions. next-on-pages spreads the cloudflare env value (where bindings are) onto
process.env
so that you can access them in your application
s
So when I say functions, I been using the functions directory to create a path to be able to see my results from D1 using the functions workers. I was unaware that you can directly access the bindings from the Native Nextjs api directories (both pages/api, and app/api), am I understanding that correctly?
I can't manually update to v1 on npm, It requires wrangler 2.20.0 and gives an error since 3 is installed.
b
Yes, you should be able to access your bindings through process.env when using next-on-pages
Ahhh yeah that peer dep should be fixed in the next release 😦
s
It said it was installing 13.3.2 when I ran the install script, but it seems to have updated itself to 13.4.3 (latest) by the time the install script had finished, something to note. Still using nextonpages v0.10.1 though
b
Anything below v1 will have a pretty janky routing system since in v1 we shipped the actual real routing system, and also much improves support for the newer nextjs versions
s
Is there a place that shows an example of this, where I can access D1 for example from a native api in Nextjs
b
I don't have an example on hand, but it's just accessing them through process.env.BINDING_NAME instead of context.env.BINDING_NAME that you'd normally do in pages functions
s
Can confirm on a fresh install, it is using the latest version of Nextjs, even tho the installer prior says its using an older version. It does that when it configures itself. I can only assume its the installer doing that and not next.

https://cdn.discordapp.com/attachments/1109565563451228210/1109620982144053348/image.png

https://cdn.discordapp.com/attachments/1109565563451228210/1109620982437642270/image.png

Will play with this, thanks.
b
Interesting, I didn't encounter that the other day 😮 nevertheless, it's the next-on-pages version not being v1 in C3 that makes me sad 😦
s
Copy code
javascript
// pages/api/hello.js

export const config = {
  runtime: "edge",
};

export default async function handler(req, res) {
  const ps = process.env.MY_DB.prepare("SELECT * from blog_posts");
  const data = await ps.all();

  return new Response(JSON.stringify(data));
}
No idea how to do this in typescript lol, maybe that should be in the docs later, some sample code for each binding
But I got it to work on 0.10.1 so thats neat
Yea, but I'm happy to see that nextonpages and pages itself is getting some love, it's been a while since we got some big updates.
b
That's a good idea, I'll add it to my todo list 👍