'Failed to connect SQL Server from Node.js using tedious

I am trying to connect to SQL Server in our domain network. I am able to connect using python but not able to connect in Node.js using Tedious.

Node.js code snippet:

     var config = {
               server: 'serverName.domain.com',
               authentication: {
                       type: 'default',
                       options: {
                         userName: 'DOMAINID\\username',
                         password: 'password'
                                 }
                               },
                options: {
                      database: 'dbName',
                      port: 1234,
                          }
              };

var connection = new Connection(config);

  connection.on('connect', function (err) {
    if (err) {
      console.log('err', err);
    } else {
     console.log("Connected");
     executeStatement();
   } 
 }); 
connection.connect();

Receiving error:

Login Failed for the user DOMAINID/username. The login is from an untrusted domain and cannot be used with Windows authentication.

But when trying to connect from Python, I am able to connect successfully.

Python snippet:

import sqlalchemy

conn = sqlalchemy.create_engine('mssql+pymssql://DOMAINID\\username:[email protected]:1234/dbName')
print(conn.execute('SELECT * FROM table_name').fetchall())

Data received successfully in python.

And also I tried with mssql and msnodesqlv8 with Microsoft ODBC 11 for Microsoft SQL Server drivers.

I am able to connect. Following is the code snippet.

const sql = require("mssql/msnodesqlv8");

const main = async () => {
const pool = new sql.ConnectionPool({
    server: "server.domain.com",
    database: "dbName",
    port: 1234,
    user:'DomainId\\username',  // Working without username and password 
    password:'password',
    options: {
        trustedConnection: true // working only with true
    }
});

await pool.connect();

const request = new sql.Request(pool);

const query = 'select * from table';

const result = await request.query(query);

console.dir(result);
};
main();

In the above snippet, I am able to connect without username and password but with trustedConnection true only. I am using windows authentication not SQL authentication. How can I connect using tedious js



Solution 1:[1]

What's your SQL Server version? Is it SQL 2016? Some users reported issue with tedious not being compatible with SQL 2016 (ref: Cannot connect to SQL Server with Node.js and Tedious).

Try the application with mssql npm package instead of tedious.

  • For all: The latest update from the questioner stated that it's working now using mssql instead of just tedious and it's making connections with the posted code? Before he didn't state that he's using mssql :-) cheers all.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1