Cloudflare Workers and FaunaDB

Hi, I wanna use FaunaDB in Cloudflare Workers, but when I use Client or a standalone query without the Client from the official faunadb npm package I get this error:

TypeError: Cannot read property 'custom' of undefined
    at Object.<anonymous> (worker.js:1:27123)
    at n (worker.js:1:110)
    at Object.<anonymous> (worker.js:1:5589)
    at n (worker.js:1:110)
    at Object.<anonymous> (worker.js:1:34737)
    at n (worker.js:1:110)
    at Object.<anonymous> (worker.js:6:10830)
    at n (worker.js:1:110)
    at Object.<anonymous> (worker.js:1:34594)
    at n (worker.js:1:110) at line 0, col 27121

I run the workers with wrangler dev

Can someone please help me? Thank you.

What does a “standalone query without the Client” mean, exactly? Without the client, how are you connecting to Fauna?

I just wanted to see the output of the query builder so I tried console.log(q.Query(q.Collection("users"))) but got the same error. My local FaunaDB instance is running alright. Tested with fauna dashboard.

The only code within the Fauna JavaScript driver involving custom is this:

var nodeUtil = util.isNodeEnv() ? require('util') : null

var customInspect = nodeUtil && nodeUtil.inspect.custom

See: faunadb-js/values.js at master · fauna/faunadb-js · GitHub

I wonder if the util package is not the standard Node.js package, but something custom that Cloudflare provides? We’ll have to investigate.

I tried importing util in the index.ts, but still the same error. Are you able to replicate my issue? Should I provide a repo where the error occurs?

I don’t have a Cloudflare account, currently, so I can’t attempt to replicate right now.

It’s hard to tell if the error is, indeed, coming from the Fauna JS driver, or something else in your worker code. Have you tried commenting out the Fauna bits to see if the error persists?

If that fails, a repo link would be helpful.

Yes, once I remove the Fauna code it works. Once I put it back in it stops working.

Here is the repo: GitHub - YamiteruXYZ/cloudflare-workers-faunadb

Thanks for sharing the repo link. I’ve filed an issue for our engineering team to investigate.

1 Like

Thank you for sharing the repo. It’s a known issue that has been fixed but not released yet. to be more detailed, Cloudflare workers don’t have a window object that we used to relly to verify nodejs/browser envirnment. here is the fix Revert abort controller and more sophisticated nodejs env detection by fireridlle · Pull Request #401 · fauna/faunadb-js · GitHub

as a temporary workaround, you can install faunadb-js from the git master branch.

1 Like