Registry / database / mysql-error-codes

mysql-error-codes

JSON →
library0.1.3jsnpmunverified

Provides a comprehensive, typed mapping of MySQL server error codes to their symbolic names (e.g., ER_DUP_ENTRY) for Node.js and TypeScript projects. Current version 0.1.3 is stable but rarely updated; includes all standard MySQL error codes up to MySQL 8.0. Ships with TypeScript definitions, supports both ESM and CommonJS imports, and is dependency-free. Useful for handling MySQL errors without magic numbers.

npm install mysql-error-codes
INSTALL
IMPORT
SIG · MYSQL-ERROR-CODES
M
mysql-error-codes
databasejavascriptv0.1.3
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.

MysqlErrorCodes
import { MysqlErrorCodes } from 'mysql-error-codes'
import MysqlErrorCodes from 'mysql-error-codes'
ESM: must use named import because the export is not a default export.
MysqlErrorCodes
const { MysqlErrorCodes } = require('mysql-error-codes')
const MysqlErrorCodes = require('mysql-error-codes')
CommonJS: destructure the required object because the module exports an object with MysqlErrorCodes as a property.
ER_DUP_ENTRY
MysqlErrorCodes.ER_DUP_ENTRY
ER_DUP_ENTRY
Error codes are accessed as properties of the MysqlErrorCodes object, not as top-level exports.

Shows how to import and use MysqlErrorCodes to check for duplicate entry error (ER_DUP_ENTRY) after an INSERT failure.

import { MysqlErrorCodes } from 'mysql-error-codes'; // Example: check for duplicate entry error (MySQL error 1062) const mysql = require('mysql2/promise'); const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: process.env.DB_PASSWORD ?? '', database: 'test' }); try { await connection.execute("INSERT INTO users (id) VALUES (1)"); } catch (err) { if (err.code === MysqlErrorCodes.ER_DUP_ENTRY) { console.log('Duplicate entry!'); } else { console.log('Other error:', err.message); } } await connection.end();
Debug
Known issues
gotchaThe package exports an object with a single property 'MysqlErrorCodes'. CommonJS users must destructure: const { MysqlErrorCodes } = require('mysql-error-codes').
fix
Use destructured require or named import: const { MysqlErrorCodes } = require('mysql-error-codes');
affects: >=0.0.0
gotchaThe package does not include newer MySQL 8.x error codes added after its last release. Error codes may be missing or outdated.
fix
Check the MySQL error reference for any missing codes and contribute to the package, or use an alternative like 'mysql-error-codes' from another source.
affects: >=0.1.3
gotchaImporting the module with an incorrect path (e.g., 'mysql-error-codes/index') may cause resolution issues in bundlers.
fix
Always import from 'mysql-error-codes' without specifying a file extension.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'MysqlErrorCodes' of 'require(...)' as it is undefined.
CommonJS code tried to destructure from require without wrapping in parentheses or using the wrong export.
fix
Ensure you use const { MysqlErrorCodes } = require('mysql-error-codes'); (with parentheses).
Module not found: Can't resolve 'mysql-error-codes'
Package not installed or import path is incorrect.
fix
Run npm install --save mysql-error-codes and verify the package is in node_modules.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
mysql-error-codes — npm install mysql-error-codes · libregistry