Node.js cannot connect to SQL Server on localhost:1433 (connection refused)
ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSEDShort 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.
Fix it fast
1Find out which cause you have
Check whether anything listens on TCP port 1433 on the machine that runs SQL Server.
netstat -an | findstr 14332Match what you see, then jump to its fix
- 1
Nothing listens on 1433, though SQL Server is runningThe TCP/IP protocol is disabledFix - 2
SQL Server Express or an instance named like SQLEXPRESS, and 1433 shows nothingSQL Server is a named instance on a dynamic portFix - 3
You 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
- 1
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/IP2Fix
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 MSSQLSERVER3Verify
Port 1433 is now listening.
netstat -an | findstr 1433Confirmed by learn.microsoft.com
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 Port2Fix
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
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 empty2Fix
Clear TCP Dynamic Ports if it shows 0, set TCP Port, then restart SQL Server from the SQL Server Services list.
Restart-Service MSSQLSERVER3Verify
The port answers from the machine that runs Node.js.
Test-NetConnection localhost -Port 1433Confirmed by learn.microsoft.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix