ozzyonfire/mongodb-worker-example — explained in plain English
Analysis updated 2026-05-18
See a reproducible example of the Cloudflare Worker global-scope restriction breaking a library import.
Learn to use dynamic imports to work around startup restrictions in edge runtimes.
Use as a starting point for connecting a Cloudflare Worker to a MongoDB database.
Debug similar 'disallowed operation in global scope' errors in your own Worker project.
| ozzyonfire/mongodb-worker-example | 0xkinno/astraea | 0xkinno/halcyon | |
|---|---|---|---|
| Stars | 0 | 0 | 0 |
| Language | TypeScript | TypeScript | TypeScript |
| Setup difficulty | moderate | hard | hard |
| Complexity | 2/5 | 4/5 | 4/5 |
| Audience | developer | developer | developer |
Figures from each repo's GitHub metadata at analysis time.
Requires a real MongoDB connection string in a local .env.local file.
mongodb-worker-example is a small sample project that demonstrates a specific bug when using MongoDB's Node.js database driver inside a Cloudflare Worker, which is a small piece of code that runs on Cloudflare's edge network instead of a traditional server. The bug happens when you import the MongoDB client library at the very top of a file, outside of any function. Cloudflare Workers do not allow certain operations, like network connections, timers, or generating random values, to run while the code is first loading, before any actual request comes in. The MongoDB library's underlying data format library does exactly that kind of work as soon as it loads, so importing it at the top level makes the whole Worker crash on startup with an error about a disallowed operation in the global scope. The project shows both the broken version and the working fix side by side. The working approach imports the MongoDB library dynamically, inside the function that handles an incoming web request, rather than at the top of the file, which avoids doing that restricted work before a request arrives. By default the sample runs this working version, which connects to a MongoDB database, inserts a test document, deletes it again, and responds with Hello World. To see the actual bug happen, you switch the request handler to call the other function included in the code, the one that imports MongoDB at the top level, and restart the local development server. Doing so reproduces the exact startup crash the project exists to demonstrate. Setting the project up locally requires installing dependencies with the pnpm package manager, and creating a local environment file with your own MongoDB connection string and database name, since it needs a real MongoDB database to connect to. The README does not mention a license.
A minimal demo showing why importing MongoDB's driver at the top level breaks a Cloudflare Worker, and how a dynamic import fixes it.
Mainly TypeScript. The stack also includes TypeScript, Cloudflare Workers, MongoDB.
Setup difficulty is rated moderate, with roughly 30min to a first successful run.
Mainly developer.
This repo across BitVibe Labs
Don't trust strangers blindly. Verify against the repo.