3

我最近刚刚使用 Typescript 进入 Knex,尝试Knex.Config在我的 knexfile.ts 中使用时出现此错误。“node_modules/knex/types/index”。Knex 没有导出的成员“配置”。

以下是我的相关文件的样子:

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"
  ]
}

包.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';与最新版本的 typescript 和 knex 一起使用。我什至尝试删除 node_modules 文件夹然后运行npm cache clean --force然后重新安装所有东西,我最终得到了同样的东西。

我还应该注意,如果我转到 node_modules/knex/types/index.d.ts 它已经有大量相同类型的错误“Knex 没有导出的成员'X'”。一些示例是 QueryBuilder、CreateTableBuilder、Transaction 和其他大量示例。

任何想法,将不胜感激。

4

1 回答 1

2

哇,我已经为此奋斗了好几天,终于意识到我的 VS Code 使用的是其内置的打字稿版本,而不是我的工作区版本。我将其更改为工作区版本,现在一切正常

于 2021-07-02T14:58:13.520 回答