withastro / adapters

Home for Astro's core maintained adapters
47 stars 26 forks source link

Astro with cloudflare adapter fails to build when using firebase #186

Closed jmho closed 3 months ago

jmho commented 3 months ago

Astro Info

Astro                    v4.5.1
Node                     v18.18.0
System                   Linux (x64)
Package Manager          unknown
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Astro fails to create a production build when the project uses the Cloudflare adapter and imports firebase-admin into a server endpoint. It appears that the frontend successfully builds but the server fails to build and the following stack trace is produced:

The package "util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "stream"

    node_modules/firebase-admin/node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js:4:31:
      4 │ const ReadableStream = require('stream').Readable
        ╵                                ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

Could not resolve "fs"
  Stack trace:

The minimum reproducible steps are as follows:

  1. Create new astro project
  2. Install firebase and firebase-admin
  3. Initialize firebase with dummy configs
  4. Create a server endpoint that has import { app } from '../../../firebase/server';
  5. Run npm run build

What's the expected result?

The server build shouldn't throw any errors. When tested with the NodeJS adapter, this was the case.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-1dhscj?file=src%2Fpages%2Fapi%2Ffriends%2Findex.ts

Participation

alexanderniebuhr commented 3 months ago

@jmho thank you for submitting this issue. Cloudflare doesn't support node apis. The package/library firebase-admin uses node's stream.

Cloudflare has a nodejs_compat mode, which allows to use some node apis, but they need to be imported with the node:* prefix syntax. (https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-with-pages-functions)

I would suggest checking in with the authors of that package, if they support the node:* import syntax.

jmho commented 3 months ago

@alexanderniebuhr Thanks for the prompt response. That makes a lot of sense! I'll check in with them.

In the mean time, do you think it would make sense for me to open a PR to update the astro docs to be more explicit about this? I feel like this is a pretty major limitation on deciding to deploy with Cloudflare, as this affects not just projects using firebase but the usage of packages that don't use node:* import syntax out of the box.

I was thinking adding a blurb to the Troubleshooting section mentioning the build failing and the potential reason why could be helpful for future devs. I was also thinking editing the main docs for the Cloudflare adapter so that this limitation is more apparent when deploying with SSR could also be useful!

What do you think?

alexanderniebuhr commented 3 months ago

I can't really imagine how that would look like. I would suggest reaching out with that question to the Docs channel on our Discord. In general we don't suggest opening directly a PR to the docs repo, because it might get rejected and then you did work for nothing, so it's worth to check in on Discord first.