Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js cannot connect to SQL Server on localhost:1433 (connection refused)

ErrorConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED

Short answer

ECONNREFUSED on 1433 means nothing is listening there. On Windows the usual reasons are that the TCP/IP protocol is off, SQL Server is a named instance on a dynamic port, or the service has not been restarted after a change. Fix it in SQL Server Configuration Manager.

  • Applies to SQL Server 2022, mssql 11, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether anything listens on TCP port 1433 on the machine that runs SQL Server.

    netstat -an | findstr 1433
  2. 2Match what you see, then jump to its fix

    1. 1Nothing listens on 1433, though SQL Server is runningThe TCP/IP protocol is disabledFix
    2. 2SQL Server Express or an instance named like SQLEXPRESS, and 1433 shows nothingSQL Server is a named instance on a dynamic portFix
    3. 3You changed the port and ECONNREFUSED stays, or only some addresses answerThe fixed port was set but the service was not restarted, or the IP entry is disabledFix

What does the error look like?

  • ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED

Cause 1 of 3

The TCP/IP protocol is disabled

1Check

Setup installs all network protocols, but they might not be enabled. In SQL Server Configuration Manager open SQL Server Network Configuration, then Protocols for your instance, and look at the state of TCP/IP.

# shown in SQL Server Configuration Manager, no command needed
# SQL Server Network Configuration > Protocols for MSSQLSERVER > TCP/IP

2Fix

Right-click TCP/IP and choose Enable, then restart the SQL Server service (and the SQL Server Browser service for a named instance).

# PowerShell, default instance, run as administrator
Restart-Service MSSQLSERVER

3Verify

Port 1433 is now listening.

netstat -an | findstr 1433

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

SQL Server is a named instance on a dynamic port

1Check

The default instance listens on 1433. Named instances use dynamic ports, so they pick a free port when the service starts.

# SQL Server Configuration Manager > TCP/IP Properties > IP Addresses > IPAll
# look at TCP Dynamic Ports and TCP Port

2Fix

Either connect by instance name, with the SQL Server Browser service running (do not set a port in that case), or give the instance a fixed port in IPAll and connect to that port.

const config = {
  server: 'localhost',
  options: { instanceName: 'SQLEXPRESS', trustServerCertificate: true },
};

3Verify

Connect with the pool.

const pool = await sql.connect(config);
console.log('connected');

Confirmed by github.com

Did this fix it?

Cause 3 of 3

The fixed port was set but the service was not restarted, or the IP entry is disabled

1Check

In IP Addresses, an IPn section with Enabled set to No is ignored even if it has a port. When Listen All is Yes, only the IPAll values count. The Database Engine starts listening on a new port only after a restart.

# SQL Server Configuration Manager > TCP/IP Properties
# IPAll: TCP Port = 1433, TCP Dynamic Ports empty

2Fix

Clear TCP Dynamic Ports if it shows 0, set TCP Port, then restart SQL Server from the SQL Server Services list.

Restart-Service MSSQLSERVER

3Verify

The port answers from the machine that runs Node.js.

Test-NetConnection localhost -Port 1433

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Give each instance a fixed port in IPAll and use that port in the connection config.
  • Restart the Database Engine after any network setting change.
  • Keep connection settings (server, port or instanceName) in environment variables so dev and production differ cleanly.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.