Registry / database / cf-mongodb-polyfills

cf-mongodb-polyfills

JSON →
library1.0.1jsnpmunverified

Cloudflare Worker polyfill for net, tls, and dns modules to enable MongoDB connections inside workerd runtime. Version 1.0.1 provides polyfills leveraging cloudflare:sockets and module aliasing in wrangler.toml. This is the only package specifically addressing MongoDB connectivity in Cloudflare Workers without custom TCP implementation. Requires wrangler module aliasing configuration.

npm install cf-mongodb-polyfills
INSTALL
IMPORT
SIG · CF-MONGODB-POLYFIL
C
cf-mongodb-polyfills
databasejavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

net polyfill
import {} from '@jchoi2x/cf-mongodb-polyfills/net'; // via alias in wrangler.toml
import { net } from 'cf-mongodb-polyfills';
Not imported directly; used via module alias. The alias maps 'net' to this package's net module.
tls polyfill
// aliased in wrangler.toml: 'tls' = '@jchoi2x/cf-mongodb-polyfills/tls'
import { tls } from 'cf-mongodb-polyfills';
Similar to net; aliased, not imported directly.
dns polyfill
// aliased in wrangler.toml: 'dns' = '@jchoi2x/cf-mongodb-polyfills/dns'
import { dns } from 'cf-mongodb-polyfills';
Aliased as well.
MongoClient (using polyfills)
import { MongoClient } from 'mongodb';
MongoClient usage remains unchanged; polyfills work via aliasing.

Demonstrates wrangler.toml aliasing and using MongoClient in a Cloudflare Worker with the polyfill.

// wrangler.toml [alias] "net" = "@jchoi2x/cf-mongodb-polyfills/net" "dns" = "@jchoi2x/cf-mongodb-polyfills/dns" "tls" = "@jchoi2x/cf-mongodb-polyfills/tls" // src/index.ts import { MongoClient } from 'mongodb'; export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { const uri = process.env.MONGODB_URI ?? 'mongodb+srv://user:pass@cluster.mongodb.net/myDB?retryWrites=true&w=majority'; const client = new MongoClient(uri); await client.connect(); const db = client.db('test'); const users = await db.collection('users').find({}).limit(10).toArray(); await client.close(); return Response.json({ users }); }, } satisfies ExportedHandler<Env>;
Debug
Known issues
breakingModule aliasing required: without [alias] section in wrangler.toml, imports of 'net', 'tls', 'dns' will fail at runtime.
fix
Add the alias configuration to wrangler.toml as shown in quickstart.
affects: >=1.0.0
gotchaOnly works with Cloudflare Workers (workerd). Not compatible with Node.js, Deno, or browsers.
fix
Use only inside Cloudflare Workers with wrangler.
affects: >=1.0.0
gotchaPackage is scoped under @jchoi2x/cf-mongodb-polyfills in the alias path, not the install name 'cf-mongodb-polyfills'.
fix
In wrangler.toml alias, use '@jchoi2x/cf-mongodb-polyfills/...' not 'cf-mongodb-polyfills/...'.
affects: >=1.0.0
deprecatedThe package is in early stages (v1.0.1). APIs may change without notice.
fix
Pin version in package.json and test upgrades.
affects: >=1.0.0
Errors
Common errors & fixes
Error: The module "net" cannot be resolved. Did you mean to enable the nodejs_compat compatibility flag?
Missing wrangler.toml alias for 'net'.
fix
Add [alias] section with 'net' = '@jchoi2x/cf-mongodb-polyfills/net'
TypeError: net.createConnection is not a function
Node.js compatibility flags enabled but no alias pointing to polyfill.
fix
Add the alias in wrangler.toml instead of relying on nodejs_compat alone.
Cannot find module '@jchoi2x/cf-mongodb-polyfills/net'
Package not installed or alias path typo (missing '@jchoi2x/' prefix).
fix
Run 'npm install cf-mongodb-polyfills' and ensure alias uses '@jchoi2x/cf-mongodb-polyfills/net'.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
1
Resources
cf-mongodb-polyfills — npm install cf-mongodb-polyfills · libregistry