配置Knex连接Supabase(PostgreSQL)时遇TypeError: Knex is not a function
配置Knex连接Supabase(PostgreSQL)时遇到"Knex is not a function"错误分析
问题详情
执行Knex相关操作时出现以下错误:
Requiring external module ts-node/register Knex is not a function TypeError: Knex is not a function at Object.<anonymous> (C:\LAVORO\MagickML\Magick\apps\server\node_modules\knex\knex.js:32:14) at Module._compile (node:internal/modules/cjs/loader:1275:14) at Module._extensions..js (node:internal/modules/cjs/loader:1329:10) at Object.require.extensions.<computed> [as .js] (C:\LAVORO\MagickML\Magick\apps\server\node_modules\ts-node\src\index.ts:1608:43) at Module.load (node:internal/modules/cjs/loader:1133:32) at Function.Module._load (node:internal/modules/cjs/loader:972:12) at Module.require (node:internal/modules/cjs/loader:1157:19) at require (node:internal/modules/helpers:119:18) at initKnex (C:\LAVORO\MagickML\Magick\apps\server\node_modules\knex\bin\cli.js:83:16) at async Command.<anonymous> (C:\LAVORO\MagickML\Magick\apps\server\node_modules\knex\bin\cli.js:247:26)
当前使用的Knex配置文件:
const dotenv = require('dotenv'); const Knex = require('knex'); dotenv.config(); const dbType = process.env.DATABASE_TYPE || 'pg'; const dbURL = process.env.DATABASE_URL || 'db.c*x*ah*ex*thvmin*ngo.supabase.co'; const configs = { pg: { client: dbType, connection: { host: process.env.DB_HOST, port: Number(process.env.DB_PORT), user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, ssl: { rejectUnauthorized: false, ca: process.env.SUPABASE_CA_CERT, }, }, }, }; const knex = Knex (configs[dbType]); knex.knex = knex; knex.default = knex; module.exports = knex;
.env文件内容:
# Database DB_CLIENT=pg DB_HOST=db.*******************.supabase.co DB_PORT=**** DB_USER=***** DB_PASSWORD=***** DB_NAME=******
已指定Knex版本为2.4.2,.env配置确认无误,但问题仍存在。
错误原因分析
- Knex模块导入方式错误:Knex v2+版本采用ES模块规范,转成CommonJS后,直接
require('knex')得到的是一个包含default属性的对象,而非直接的构造函数,调用Knex(config)自然会报错"不是函数"。 - 配置文件不符合Knex CLI要求:Knex CLI执行命令时,会自动加载
knexfile.js并期望其导出配置对象,而非初始化后的Knex实例。当前配置文件直接导出了实例,导致CLI内部初始化Knex时出现逻辑错误。
解决方案
1. 修正Knex导入方式
将导入语句改为取default导出:
const Knex = require('knex').default;
2. 调整配置文件结构(适配CLI与程序调用)
如果需要同时支持Knex CLI命令和程序内直接使用Knex实例,建议拆分配置:
- 首先让
knexfile.js导出纯配置对象:
const dotenv = require('dotenv'); dotenv.config(); const dbType = process.env.DB_CLIENT || 'pg'; // 统一使用.env中的DB_CLIENT变量 module.exports = { [dbType]: { client: dbType, connection: { host: process.env.DB_HOST, port: Number(process.env.DB_PORT), user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, ssl: { rejectUnauthorized: false, ca: process.env.SUPABASE_CA_CERT, }, }, }, };
- 新建一个
db.js文件专门初始化Knex实例,供程序调用:
const Knex = require('knex').default; const config = require('./knexfile')[process.env.DB_CLIENT || 'pg']; const knex = Knex(config); module.exports = knex;
3. 补充依赖与变量检查
- 确保已安装PostgreSQL驱动:
npm install pg - 确认.env中的
SUPABASE_CA_CERT变量已正确配置(如果需要自定义CA证书),若不需要可移除ca配置项,仅保留rejectUnauthorized: false即可连接Supabase。
内容的提问来源于stack exchange,提问作者Coding4Sun
相关产品推荐
相关产品推荐

