How to connect to SQL Server with windows authentication from Node.JS using mssql module How to connect to SQL Server with windows authentication from Node.JS using mssql module sql-server sql-server

How to connect to SQL Server with windows authentication from Node.JS using mssql module


Since this is a fairly visible answer, I wanted to add in a code snippet that worked for me with Trusted Connection. Got to it from getglad's edited answer.

const sql = require("mssql");require("msnodesqlv8");const conn = new sql.Connection({  database: "db_name",  server: "server_name",  driver: "msnodesqlv8",  options: {    trustedConnection: true  }});conn.connect().then(() => {  // ... sproc call, error catching, etc  // example: https://github.com/patriksimek/node-mssql#request});

Using trusted connection, I was able to execute stored procedures, log the output, and close the connection without any trouble, and msnodesqlv8 has been updated more recently than any of the other drivers (latest release was October 2016 as of 11/3/2016), so that seems to be a safe choice as well.

And here's an example using mssql@4.0.4. The only changes are the initial require, which pull in msnodesqlv8 from within mssql, and sql.Connection is now sql.ConnectionPool. You will also need to change your stored procedure calls since the response is different, noted here. Credit to Jon's answer since he updated mine before I did!

const sql = require("mssql/msnodesqlv8");const conn = new sql.ConnectionPool({  database: "db_name",  server: "server_name",  driver: "msnodesqlv8",  options: {    trustedConnection: true  }});conn.connect().then(() => {  // ... sproc call, error catching, etc  // example: https://github.com/patriksimek/node-mssql#request});


I have never been able to get mssql + windows auth to work for any of my projects. Try edge and edge-sql - it has worked for me. Be sure you install all the required packages.

https://github.com/tjanczuk/edge

https://github.com/tjanczuk/edge-sql

From there, it's pretty steamlined.

var edge = require('edge');var params = {  connectionString: "Server=YourServer;Database=YourDB;Integrated Security=True",  source: "SELECT TOP 20 * FROM SampleData"};  var getData = edge.func( 'sql', params);getData(null, function (error, result) {   if (error) { console.log(error); return; }   if (result) {    console.log(result);   }   else {    console.log("No results");   } });

EDIT

Well... 10 days after my original answer, apparently mssql added Windows Auth to the package. They heard our cries :) See here. I have not tested it yet, but it is officially in my backlog to test integration. I will report back.

FWTW, if mssql fits your needs, I would go with it, as 1) edge-sql has been dormant for 2 years and 2) the primary contributor has said he has left projects like this "in the caring hands of Microsoft", since he no longer works there.

EDIT 2

This keeps getting upvotes and there are comments saying some of the other answers' code examples either aren't working or aren't working on Windows.

This is my code using mssql, working on Windows, with msnodesqlv8 also installed:

var sql = require('mssql/msnodesqlv8');var config = {  driver: 'msnodesqlv8',  connectionString: 'Driver={SQL Server Native Client XX.0};Server={SERVER\\NAME};Database={dbName};Trusted_Connection={yes};',};sql.connect(config).then(function() { ...profit...}).catch(function(err) {  // ... connect error checks});


I have been struggling too for some time about how to use mssql + Windows Auth, here is how i got it to work on my project.

As pointed out in the mssql documentation, you need msnodesqlv8 installed too.

npm install msnodesqlv8

Now, following on Aaron Ballard's answer, you use it like this:

const sql = require('mssql/msnodesqlv8')const pool = new sql.ConnectionPool({  database: 'database',  server: 'server',  driver: 'msnodesqlv8',  options: {    trustedConnection: true  }})pool.connect().then(() => {  //simple query  pool.request().query('select 1 as number', (err, result) => {        console.dir(result)    })})

As a note, i tried to add this as a comment on Aaron's answer, as mine is just a complement/update to his, but i don't have enough reputation to do so.