Typescript Knex Missing Exported Members

Viewed 1352

I'm just getting into Knex recently using Typescript and I am getting this error when trying to use Knex.Config in my knexfile.ts. "node_modules/knex/types/index".Knex has no exported member 'Config'.

Here are what my relevant files look like:

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

I've looked at many different posts and nothing seems to be working. Including making sure the import is import { Knex } from 'knex'; so that it works with the latest version of typescript and knex. I've even tried deleting the node_modules folder then running npm cache clean --force then reinstalling everything and I'm ending up with the same thing.

I should also note if I go to node_modules/knex/types/index.d.ts it already has a ton of the same type of error "Knex has no exported member 'X'". Some examples are QueryBuilder, CreateTableBuilder, Transaction, and tons of others.

Any ideas would be appreciated.

2 Answers

Wow, I've been fighting with this for days and finally realized that my VS Code was using its built-in typescript version rather than my workspace version. I changed it to workspace version and it all works now

Supplement to Jordan9232's answer, provide detail instruction by screen capture

click Typerscript version number in right hand corner

select workspace version

Related