2

I'm trying to connect to SQL Server:

var sql = require("mssql");

var dbConfig = {
    server: "LAP12\\INSTANCE1",
    database: "SampleDb",
    port: 1433,
    options: {
        trustedConnection: true
    }
};

// connect to your database
sql.connect(dbConfig, function (err) {

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from SampleTable', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

    });
});

But I receive this error:

ConnectionError: Port for INSTANCE1 not found in ServerName...

I've tried to follow instructions here https://github.com/patriksimek/node-mssql/issues/130 , but that didn't help. TCP is enabled.

Changing config to this didn't help either:

var dbConfig = {
    server: "LAP12",
    port: 1433,
    options: {
        instanceName: 'INSTANCE1',
        database: 'SampleDb',
        trustedConnection: true,
    }
};
0

5 Answers 5

2

Ok, I had the same issue, will try to help. this is my config exemple

const config = {
    user: 'sa',
    password: '****',
    server: 'DESKTOP-Q5TO47P',
    database: 'dbname',
    options: {           
        encrypt: false
    }
};

You need to turn on the SQL Server Browser. Go to start up menu or the search and look for SQL Server Configuration Manager. Run it! (Im using 2018 version)

  • In the left Tab click on SQL Server Services
  • now in the right tab double click on SQL Server Browser
  • will open a window, you will see 3 tabs, go for the Service tab
  • change start mode to Automatic and apply
  • left click on SQL Server Browser and click restart

  • Back to the right tab click on SQL Server Network Configuration

  • then Client Protocols
  • change TCP/IP to enable

Let me know if it works.

2
  • this worked for me on Windows 10 with SQL Server 2014; thank you
    – xinthose
    Commented Apr 9, 2020 at 16:58
  • Made IPAll => Clear TCP Dynamic Ports, set TCP Port 1433 worked for me
    – aads
    Commented Jun 23, 2021 at 17:16
1

It worked perfectly for me on SQL Server 2022 and NodeJS v18.17.1. Just don't forget to setup the port on your TCP/IP connection.

Menu: SQL Server Configuration Manager > SQL Server Network Configuration > TCP/IP > Properties > IP Address > TCP Port: 1433

This is my DB Config setting on NodeJS:

const dbconfig = {
  user: "YourUser", 
  password: "YourPass",
  server: "YourServer", 
  port: 1433, 
  database: "YourDatabase",
  options: {
    trustServerCertificate: true, 
  }
}

Hope it helps!

0

after adding this encrypt: false, application not showing any error neither responding, Its not connecting to DB

0

In my case the "SQL Browser" is not reachable because the firewall port is not open. Solution for me is to open the port or change dbConfig to (no instance name, no port):

var dbConfig = {
    server: "LAP12",
    database: "SampleDb",
    options: {
        trustedConnection: true
    }
};

The port is still closed to SQL-Browser, with that change I can connect to my DB.

My nodejs Package 'mssql'

0

you have to set the port number in the sql server Management Studio Management Studio => TCP/IP enter image description here

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 19, 2023 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.