Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
mysql
✓ const mysql = require('database-js-mysql2');
✗ import mysql from 'database-js-mysql2';
The package primarily targets CommonJS environments. ESM imports are not directly supported.
Connection
✓ const { Connection } = require('database-js');
✗ import { Connection } from 'database-js';
When used with `database-js`, the Connection class is imported from the parent `database-js` package, which also targets CommonJS.
Database
✓ const Database = require('database-js').Connection;
✗ import Database from 'database-js/Connection';
Common aliasing pattern for the `database-js` Connection class in CommonJS.
Demonstrates connecting to MySQL using `database-js` with the `database-js-mysql2` driver, including parameterized queries and SSL configuration via connection string. Uses environment variables for credentials.
const Database = require('database-js').Connection;
const fs = require('fs');
// Dummy 'fs' for example to make it runnable without actual files
// In a real scenario, these files would exist on disk.
const caCertPath = './path/to/ca.pem';
const keyPath = './path/to/key.pem';
const certPath = './path/to/cert.pem';
// Mock fs.existsSync and fs.readFileSync for demonstration purposes
const originalExistsSync = fs.existsSync;
const originalReadFileSync = fs.readFileSync;
fs.existsSync = (path) => path === caCertPath || path === keyPath || path === certPath;
fs.readFileSync = (path) => Buffer.from(`mock_certificate_for_${path}`);
(async () => {
let connection, statement, rows;
const dbUser = process.env.DB_USER ?? 'my_secret_username';
const dbPass = process.env.DB_PASSWORD ?? 'my_secret_password';
const dbHost = process.env.DB_HOST ?? 'localhost';
const dbPort = process.env.DB_PORT ?? 3306;
const dbName = process.env.DB_NAME ?? 'my_top_secret_database';
// Example connection string with SSL parameters, ensuring paths are URL-encoded
// In a real application, ensure your SSL files are correctly placed and permissions set.
const connectionString = `mysql2://${dbUser}:${dbPass}@${dbHost}:${dbPort}/${dbName}?ssl[ca]=${encodeURIComponent(caCertPath)}&ssl[key]=${encodeURIComponent(keyPath)}&ssl[cert]=${encodeURIComponent(certPath)}`;
try {
connection = new Database(connectionString);
statement = await connection.prepareStatement("SELECT ? AS user_name_col, ? AS value_col");
rows = await statement.query('not_so_secret_user', 123);
console.log('Query Result:', rows);
} catch (error) {
console.error('Database Error:', error);
} finally {
if (connection) {
await connection.close();
}
}
})();
// Restore original fs methods if necessary in a larger application context
fs.existsSync = originalExistsSync;
fs.readFileSync = originalReadFileSync;
Errors
Common errors & fixes
Error: Cannot find module 'database-js'
The `database-js` core package is not installed, but `database-js-mysql2` expects it when used as a driver.
fixInstall the `database-js` package: `npm install database-js`.
TypeError: Cannot read properties of undefined (reading 'Connection')
Attempting to destructure `Connection` from `require('database-js')` when `database-js` might not be correctly installed or resolved, or `Connection` is not a named export in an unexpected scenario.
fixVerify `npm list database-js` shows the package, and ensure the import is `const Database = require('database-js').Connection;` if you are using the older `database-js` pattern. Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'host' (using password: YES/NO)
Incorrect MySQL username, password, or host settings in the connection string. The database server rejected the authentication attempt.
fixDouble-check your `DB_USER`, `DB_PASSWORD`, `DB_HOST`, and `DB_PORT` environment variables or hardcoded values. Ensure the user has correct privileges for the specified database.
Audit
Dependencies
database-jsoptionalRequired as the core abstraction layer when using this package as a database-js driver.
mysql2requiredThe underlying MySQL client library that this package wraps.