【发布时间】:2021-09-14 11:11:10
【问题描述】:
我最近刚刚使用 Typescript 进入 Knex,但在我的 knexfile.ts 中尝试使用 Knex.Config 时出现此错误。 "node_modules/knex/types/index".Knex 没有导出的成员 'Config'。
我的相关文件如下所示:
knexfile.ts:
import { Knex } from 'knex';
const config: Knex.Config = {
client: 'mssql',
connection: {
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
timezone: 'utc'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations',
directory: 'migrations'
}
};
export default config;
tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"isolatedModules": true
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
package.json:
{
"name": "migrations",
"version": "1.0.0",
"private": true,
"scripts": {
"migrate:make": "knex --knexfile src/knexfile.ts -x ts migrate:make",
"migrate:latest": "knex --knexfile src/knexfile.ts migrate:latest",
"migrate:rollback": "knex --knexfile src/knexfile.ts migrate:rollback"
},
"dependencies": {
"knex": "^0.95.6",
"mssql": "^7.1.3",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
},
"devDependencies": {
"@types/knex": "^0.16.1",
"@types/mssql": "^7.1.0",
"@types/node": "^15.14.0"
}
}
我查看了许多不同的帖子,但似乎没有任何效果。包括确保导入是import { Knex } from 'knex';,以便它可以与最新版本的打字稿和knex一起使用。我什至尝试删除 node_modules 文件夹,然后运行 npm cache clean --force 然后重新安装所有内容,我最终得到了同样的结果。
我还应该注意,如果我转到 node_modules/knex/types/index.d.ts 它已经有大量相同类型的错误“Knex 没有导出的成员 'X'”。一些示例包括 QueryBuilder、CreateTableBuilder、Transaction 和其他大量示例。
任何想法都将不胜感激。
【问题讨论】:
标签: typescript knexjs