Error: Cannot find module './dialects/postgres' when upgrading Knex.js
0 reputation · 12 Nov 2020, 15:29 UTC
0 reputation · 12 Nov 2020, 15:29 UTC
After upgrading Knex.js from a 0.x release to 1.x, the application fails to initialize because the internal dialect module cannot be located at the former path 'knex/lib/dialects/postgres'. The goal is to restore a working Knex instance by ensuring the resolver points to the new location 'knex/dialects/postgres' while preventing stale files from being loaded.
Additionally, there is no consensus on how Knex should handle .whereIn(column, []): some versions throw a validation error whereas others return an empty result set, and the documentation does not state the intended behavior. This ambiguity affects query reliability when empty arrays are passed inadvertently.
What steps should be taken to update the module path after upgrading Knex from 0.x to 1.x? How can developers verify that the dialect module resolves correctly after the upgrade? What is the expected behavior of .whereIn with an empty array in Knex?
The Cannot find module './dialects/postgres' error occurs because Knex 1.x moved dialect files from knex/lib/dialects/ to knex/dialects/. This is an internal restructuring—your code should not reference these paths directly.
npm install knex@latest
rm -rf node_modules package-lock.json
npm install
client option, not a manual dialect require:
const knex = require('knex')({
client: 'pg',
connection: process.env.DATABASE_URL
});
Run this minimal script to confirm the PostgreSQL dialect loads without module errors:
const knex = require('knex')({ client: 'pg' });
console.log('Dialect loaded:', knex.client.dialect);
// Should output: Dialect loaded: postgres
If this prints postgres without throwing, the resolver is working. No further path configuration is needed.
In Knex 1.x, .whereIn('column', []) generates a WHERE 1=0 clause (or equivalent), causing the query to return zero rows. It does not throw a validation error. This behavior is intentional and matches standard SQL semantics for an empty IN list.
Example generated SQL (PostgreSQL):
select * from "users" where "id" in (select 1 where false)
If the error persists after the steps above, run npm list knex and confirm the output shows knex@1.x.x (not a 0.x version) and that no duplicate Knex copies exist in nested node_modules.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 12 Nov 2020, 16:15 UTC
When upgrading from Knex 0.x to 1.x, the Cannot find module error usually stems from manual require calls targeting the internal lib directory. In version 1.x, dialect implementations were restructured. To resolve this, avoid importing internal paths and instead use the client configuration property (e.g., client: 'pg'), which allows Knex to handle the internal resolution automatically.
Regarding .whereIn(column, []), Knex 1.x generally handles empty arrays by generating a condition that evaluates to false (such as WHERE 1=0) to ensure no rows are returned without crashing. You can verify the exact SQL generated for your specific dialect by running:
knex('table').whereIn('column', []).toSQL().toNative();
If the module error persists after updating the config, a clean reinstall (removing node_modules and the lockfile) is recommended to clear legacy 0.x directory structures.