TypeError: sql.Connection is not a function Node.js

11,507

In fact, the function Connection doesn't exist. According to the documentation, to connect to a database, you can use the class ConnectionPool, or the method connect.

var dbConn = new sql.ConnectionPool(config);

dbConn.connect(function(err) {
    // ...
});

// OR ...

sql.connect(config).then(function(dbConn) {
   // ...
})
Share:
11,507
Jhonycage
Author by

Jhonycage

Updated on July 09, 2022

Comments

  • Jhonycage
    Jhonycage almost 2 years

    I am very beginner with node.js and javscript itself. I am trying to establish a basic connection to SQLServer but I am getting the following error:

    TypeError: sql.Connection is not a function

    I am following the recommendations of the mssql package git repository

    //get an instance of sqlserver 
    var sql = require('mssql');
    
    //set up a sql server credentials
    var config = {
    server: 'localhost',
    database: 'trafiyadb',
    user: 'jhonycage',
    password:  'juan1014',
    port: 1433
    };
    
    
    function con() {
    var dbConn = new sql.Connection(config);
    dbConn.connect().then(function(){
        console.log("connected")
    }).catch(function (err) {
        console.log(err);
    })
    }
    
    con();
    

    and this is the package.json

    {
     "name": "trafiyaapi",
     "version": "1.0.0",
     "description": "",
     "main": "server.js\u001b[A\u001b[B\u001b[B\u001b[B\u001b[A\u001b[B",
     "dependencies": {
     "express": "^4.15.2",
     "body-parser": "^1.17.1",
     "mssql": "^4.0.4",
     "jsonwebtoken": "^7.4.0"
     },
     "devDependencies": {},
     "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
     "start": "node server.js"
     },
     "author": "",
     "license": "ISC"
     }
    

    I am just executing

    $node server

    I must be doing something wrong, how can i connect to database?