Connecting to MSSQL With Windows Authentication in Nodejs

  • Install npm package
npm install mssql msnodesqlv8 --save
  • Sample code
const sql = require('mssql/msnodesqlv8');

var dbConfig = {
    connectionTimeout : 30000,
    database: "<DB Name>",
    server: "<server Name>",
    options: {
        trustedConnection: true,
    }
};

var dbConfig2 = {
    connectionTimeout : 30000,
    connectionString: 'Driver={SQL Server Native Client 11.0};Server=<Server Name>;Database=<DB Name>;Trusted_Connection=yes;',
    options: {
        
    }
};

(async () => {
    try {
        console.log("start");
        await sql.connect(dbConfig);  // both format of dbConfig or dbConfig2 will work
        const result = await sql.query(`select SYSDATETIME() as dt `);
        console.dir(result.recordset);
        console.log("end");
    } catch (err) {
        // 
        console.log(err);
    } finally {
        //
    }
})();