quack
quack

Reputation: 555

Node js knex and mssql error - 'TypeError: _this.driver.Connection is not a constructor'

Not a question, more of a heads up in case anyone else encounteres this - While setting up a new node project, I want to use knex to access a MSSQL database server. I'm initializing knex with this:

{
client: 'mssql',
connection: {
    driver: 'msnodesqlv8',
    server: 'localhost',
    database:'testdb'
    options: {
        trustedConnection: true
    }
}

I get the error: 'TypeError: _this.driver.Connection is not a constructor'. After a lot of googling, I couldn't find anything with this error message, so I thought I'd post a heads up here.

I installed the latest knex, mssql, and msnodesqlv8 packages. And that's the problem. As of mssql v4.x, knex and mssql don't play nice (for now). I was able to fix this by downgrading the mssql package to 3.3.

Upvotes: 3

Views: 3370

Answers (2)

Owen Allen
Owen Allen

Reputation: 11958

I had this same problem in a TypeScript project, and the ultimate culprit was that I had added a folder into my project that had the same name as the official npm package for the Knex client. For example, I was using client : "pg" which meant that internally it did a require("pg") to load that client.

My project structure was like so:

/app/
  node_modules/
  src/
    pg/
      index.ts -> my personal pg connector library
    ... other folders and files
  package.json
  tsconfig.json

The root culprit was that I had an improper baseUrl in my /app/tsconfig.json. I had used "baseUrl" : "./src" which meant it was searching in the /app/src for pg and finding my own library, rather than looking in /app/node_modules as one would expect. Upon changing the baseUrl to ./ the error went away.

Upvotes: 0

Urasquirrel
Urasquirrel

Reputation: 1585

The answer is exactly as is stated at the end of the question.

"I installed the latest knex, mssql, and msnodesqlv8 packages. And that's the problem. As of mssql v4.x, knex and mssql don't play nice (for now). I was able to fix this by downgrading the mssql package to 3.3."

I was able to solve the problem successfully with this solution also.

Upvotes: 3

Related Questions