How to use Cloudflare Workers with the Firestore Node.js Module?

Hello everybody,

I am trying to write a Cloudflare Worker that interacts with the Google Firestore Database.

This is the quick-start guide I followed:
https://firebase.google.com/docs/firestore/quickstart

So, I installed the firebase-admin package with npm
npm install firebase-admin --save

And then I used this code for testing:

const admin = require('firebase-admin');

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
/**
 * Respond with hello worker text
 * @param {Request} request
 */
async function handleRequest(request) {
  return new Response('Hello worker!', {
    headers: { 'content-type': 'text/plain' },
  })
}

Here is my wrangler.toml:

name = "firestore-demo"
type = "webpack"
account_id = ""
workers_dev = true
route = ""
zone_id = "

And this is the (Huge) error-message I’m getting when running wrangler preview :sweat_smile: (Unfortunately it’s to big to include it directly in this post):
https://pastebin.com/92PxBbJH

Does anyone habe an idea how to get Firebase and Cloudflare Workers working together without using the “plain” REST API of Firestore?

Any help is much appreciated! :blush:

Thank you!

Best
Rick

The simple answer is that Workers is not a Node environment so you can’t “install” node packages. You can import them, but you need to copy the actual source within the file itself. It’s a simple JS runtime, so the Workers’ code must be bundled as if you are shipping to a browser (it’s the actual engine underneath Chrome with some slight changes).

Hi Matteo,

got it thanks!

Then I guess I will have to go with the REST API of Firestore. :slight_smile:

Rick

1 Like