Registry / database / knex
library3.2.10jsnpmunverified

Knex.js is a batteries-included SQL query builder for Node.js supporting PostgreSQL, MySQL, CockroachDB, MSSQL, SQLite3, and Oracle. The current stable version is 3.2.10, released with a monthly cadence. Key differentiators include a unified API across dialects, transaction support, connection pooling, streaming queries, and a comprehensive schema builder. TypeScript types are shipped. Requires Node.js >=16. Optional peer dependency pg-query-stream for PostgreSQL streaming.

npm install knex
INSTALL
IMPORT
SIG · KNEX
K
knex
databasejavascriptv3.2.10
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.

knex (default)
import knex from 'knex'
const knex = require('knex').default
ESM default import; CommonJS users use const knex = require('knex')
knex (CommonJS)
const knex = require('knex')
const { knex } = require('knex')
CommonJS default export is the function itself.
Knex type
import { Knex } from 'knex'
import Knex from 'knex' (for type)
TypeScript type for Knex instance and config.

Creates a Knex instance connecting to PostgreSQL, creates a table, inserts a row, and queries it.

import knex from 'knex'; const db = knex({ client: 'pg', connection: { host: process.env.DB_HOST ?? 'localhost', user: process.env.DB_USER ?? 'user', password: process.env.DB_PASS ?? '', database: process.env.DB_NAME ?? 'test' } }); async function main() { // Create table await db.schema.createTable('users', (table) => { table.increments('id'); table.string('name'); table.integer('age'); }); // Insert await db('users').insert({ name: 'Alice', age: 30 }); // Query const users = await db('users').select('*'); console.log(users); await db.destroy(); } main().catch(console.error);
Debug
Known issues
breakingIn v2, the default export changed from a function to a callable object. Using require('knex').default is no longer valid in CommonJS.
fix
Use require('knex') for CommonJS or import knex from 'knex' for ESM.
affects: >=2.0.0
breakingIn v3, the minimum Node.js version was raised to 16.
fix
Update Node.js to >=16.
affects: >=3.0.0
deprecatedOracle dialect has been deprecated in v3.5 and will be removed in the future.
fix
Migrate to a supported dialect or use a dedicated Oracle client.
affects: >=3.5.0
gotchaWhen using ES modules, a .default property is not available on the import. Common mistake is to destructure knex.
fix
Use import knex from 'knex' (not import { knex } from 'knex').
affects: >=2.0.0
Errors
Common errors & fixes
Error: Cannot find module 'pg'
Missing database driver for PostgreSQL.
fix
Run: npm install pg
Unhandled rejection error: Knex: run requested a transaction, but the client has been destroyed.
Calling db.destroy() while queries are still in flight.
fix
Ensure all queries complete before destroying, or use db.destroy() after Promise.all.
TypeError: knex is not a function
Trying to call knex() without importing it correctly, or importing from wrong path.
fix
Use import knex from 'knex' (or const knex = require('knex')) and call knex(config).
Upgrade
Version history
3.2.10latest on npm
Audit
Dependencies
pg-query-streamoptionalRequired for streaming PostgreSQL queries
Agent activity
7 hits · last 30 days
node
6
Resources
packageknex
knex — npm install knex · libregistry