canirequire

canirequireCommonJS › mysql2

Is mysql2 ESM-only? Can you require() it?

fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS

Last verified Sep 9, 2026 against mysql2@3.24.4. JSON

CommonJS CommonJS — require() works on every Node.js version

mysql2 ships CommonJS only. require("mysql2") works everywhere. import "mysql2" also works because Node.js and Bun can import CommonJS from ES modules, but only the default export is guaranteed; named imports depend on Node's static analysis of the module.

Latest
3.24.4 (Sep 8, 2026)
Module format
CommonJS
Last CommonJS version
3.24.4
TypeScript types
Bundled
Node engines
>= 8.0
Weekly downloads
10.5M
License
MIT

Does mysql2@3.24.4 actually load? Runtime test results

Installed and executed on Sep 9, 2026 with require("mysql2") from a CommonJS file and import "mysql2" from an ES module.

Runtimerequire("mysql2")import "mysql2"
Node.js 18✓ works✓ works
Node.js 20✓ works✓ works
Node.js 22✓ works✓ works
Node.js 24✓ works✓ works
Node.js 26✓ works✓ works
Bun 1.4✓ works✓ works

How to import mysql2

ES modules (import)

import mysql2 from "mysql2";

Named imports from a CommonJS package only work when Node.js can statically detect them. If a named import fails, use the default import and destructure.

CommonJS (require)

const mysql2 = require("mysql2");

Module format by version

Derived from the type, main and exports fields of every stable release on npm (216 versions; the newest 60 shown).

VersionsFormatFirst published
3.9.7 – 3.24.4CommonJSApr 21, 2024

Frequently asked

Is mysql2 ESM-only?

mysql2@3.24.4 is a CommonJS package. mysql2 ships CommonJS only. require("mysql2") works everywhere. import "mysql2" also works because Node.js and Bun can import CommonJS from ES modules, but only the default export is guaranteed; named imports depend on Node's static analysis of the module.

Can I import mysql2 with ES module syntax?

Yes. import mysql2 from "mysql2" works in Node.js and Bun because they support importing CommonJS modules. Named imports work only when Node.js can statically detect the exports.

Does mysql2 work in Bun?

Yes. import "mysql2" loaded successfully in Bun 1.4 on Sep 9, 2026.

Does mysql2 ship TypeScript types?

Yes. mysql2 bundles its own type declarations; no @types package is needed.

Links

Other commonjs packages

Similar popularity