Registry / database / cloudflare-mysql

cloudflare-mysql

JSON →
library0.9.4jsnpmunverified

A modified MySQL JavaScript package adapted for Cloudflare Workers by removing Node.js-specific dependencies. Version 0.9.4 is the latest stable release, with no regular release cadence. Key differentiators: provides a familiar MySQL connection API for Workers, includes TypeScript types, but connections must be created per request (cannot persist). Strongly recommended against for new projects — consider serverless databases like Cloudflare D1 or Turso instead.

npm install cloudflare-mysql
INSTALL
IMPORT
SIG · CLOUDFLARE-MYSQL
C
cloudflare-mysql
databasejavascriptv0.9.4
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.

createConnection
import { createConnection } from 'cloudflare-mysql'
const mysql = require('cloudflare-mysql')
ESM import only; the library is designed for Cloudflare Workers which use ES modules.
createPool
import { createPool } from 'cloudflare-mysql'
import { createPool } from 'mysql'
createPool is available but has the same per-request connection limitation.
ConnectionConfig
import type { ConnectionConfig } from 'cloudflare-mysql'
TypeScript type import for configuration options, available in v0.9.4+.

Connect to MySQL from a Cloudflare Worker, run a query, return JSON response. Uses per-request connection.

import { createConnection } from 'cloudflare-mysql'; import type { Env } from './types'; // replace with actual path export default { async fetch(request: Request, env: Env, context: ExecutionContext): Promise<Response> { const result = await new Promise<any>((resolve) => { const connection = createConnection({ host: env.DB_HOST ?? '', user: env.DB_USER ?? '', password: env.DB_PASSWORD ?? '', database: env.DB_NAME ?? '' }); connection.connect((error) => { if (error) throw new Error(error.message); connection.query('SELECT 1 + 1 AS solution', (error, rows) => { connection.end(); resolve(rows); }); }); }); return new Response(JSON.stringify(result)); } };
Debug
Known issues
breakingEach Worker request must create a new connection; connections cannot be reused across requests.
fix
Use a serverless database (D1, Turso) for persistent connections. If using MySQL, expect high connection overhead.
affects: >=0.0.0
gotchaThe package only polyfills MySQL dependencies enough to be functional, not 100% of Node.js APIs.
fix
Test all MySQL features you rely on; some may not work in Workers environment.
affects: >=0.0.0
deprecatedAuthor strongly advises against using this for new projects.
fix
Consider Cloudflare D1 or Turso as alternatives.
affects: >=0.0.0
gotchaNot a full MySQL implementation – missing features like prepared statements, transactions, etc.
fix
Check the underlying mysqljs/mysql repository for supported features.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'net'
Node.js 'net' module unavailable in Cloudflare Workers.
fix
Use cloudflare-mysql which replaces 'net' with a Worker-compatible polyfill.
Error: Connection lost: The server closed the connection.
Connection is closed after request ends; subsequent requests cannot reuse same connection.
fix
Create a new connection per request.
TypeError: connection.query is not a function
Incorrect import or initialization order.
fix
Ensure you import { createConnection } and call connection.query after connection.connect callback.
Upgrade
Version history
0.9.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
OpenAI (training)
1
Resources
cloudflare-mysql — npm install cloudflare-mysql · libregistry