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.
default (driver)
✓ const dbmigrate = require('db-migrate');
const driver = dbmigrate.getInstance({ driver: 'mysql2' });
✗ const driver = require('db-migrate-mysql2');
Do not require the driver directly. Use db-migrate API with driver field set to 'mysql2'.
driver instance with options
✓ const dbmigrate = require('db-migrate');
const instance = dbmigrate.getInstance(true, { driver: 'mysql2', config: { host: 'localhost', user: 'root', password: '', database: 'test' } });
✗ const DBMigrate = require('db-migrate');
const instance = new DBMigrate({ driver: 'mysql2' });
Always use getInstance() with the driver option. Do not use constructor.
type imports (TypeScript)
✓ import type { Driver } from 'db-migrate-mysql2';
✗ import { Driver } from 'db-migrate-mysql2';
This package doesn't export runtime symbols; only types are available if using TypeScript.
Shows how to install, configure, and run a migration using db-migrate with the mysql2 driver.
// Install: npm install db-migrate db-migrate-mysql2
// database.json
{
"dev": {
"driver": "mysql2",
"user": "root",
"password": "",
"host": "localhost",
"database": "myapp_dev"
}
}
// Run migration:
// npx db-migrate up
// Programmatic usage:
const dbmigrate = require('db-migrate');
const instance = dbmigrate.getInstance(true, {
driver: 'mysql2',
config: {
host: 'localhost',
user: 'root',
password: '',
database: 'myapp_dev'
}
});
// Create migration file with up and down functions
// Up: create table
// Down: drop table
instance.create('add-users-table', (err, migration) => {
if (err) throw err;
console.log('Created:', migration.name);
});
Errors
Common errors & fixes
Error: No such driver found. Add it by installing db-migrate-<driver>
Using driver name 'mysql' instead of 'mysql2' in config, or driver package not installed.
fixInstall db-migrate-mysql2 and set driver: 'mysql2' in database.json
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: NO)
Missing or incorrect credentials in database configuration.
fixUpdate database.json with correct user, password, host, and database.
TypeError: dbmigrate.getInstance is not a function
Using an older version of db-migrate that doesn't export getInstance, or incorrect import style.
fixEnsure db-migrate version >= 0.11.0 and use const dbmigrate = require('db-migrate'); then call dbmigrate.getInstance() Error: Cannot find module 'db-migrate-base'
Missing peer dependency or incorrect npm install (npm < 7 auto-installs peers).
fixRun: npm install db-migrate-base
Audit
Dependencies
db-migrate-baserequiredrequired by db-migrate to load driver plugins
mysql2requiredactual MySQL connection and query execution