This repository demonstrates the difference between Cloudflare Vite Plugin's Node.js polyfills and Wrangler's polyfills, specifically when running the mysql2 package.
When using the mysql2 package with Cloudflare Workers, there's a notable difference in behavior between:
- Running with Vite's development server (
dev:vite) - Running with Wrangler's development server (
dev:wrangler)
The key issue manifests as a buffer.hasOwnProperty is not a function error when using Vite's development server, which can be traced back to the safer-buffer package. This error does not occur when using Wrangler's development server.
- Node.js (latest LTS version recommended)
- npm or bun
- MySQL database credentials
- Clone the repository
- Install dependencies:
npm install # or bun install
-
Start the Vite development server:
npm run dev:vite # or bun run dev:viteThen visit
http://localhost:5173/db- this will throw the buffer error. -
Start the Wrangler development server:
npm run dev:wrangler # or bun run dev:wranglerThen visit
http://localhost:8787/db- this should work correctly.
- Vite dev server (
localhost:5173/db): Should throw abuffer.hasOwnProperty is not a functionerror - Wrangler dev server (
localhost:8787/db): Should work correctly and connect to the database
- mysql2: ^3.14.0
- @cloudflare/vite-plugin: ^1.0.5
- wrangler: ^4.10.0