Skip to content
Latchkey

Nightwatch "Error retrieving a new session" connecting to 4444 in CI

Nightwatch requests a new session from the WebDriver server (a standalone Selenium, a Grid hub, or a managed driver) at the configured host and port. If nothing answers on that port, it reports "Error retrieving a new session" with a connection-refused cause.

What this error means

The run fails before any test with "Error retrieving a new session from the selenium server" and "connect ECONNREFUSED 127.0.0.1:4444" or a similar connection error.

nightwatch
Error retrieving a new session from the selenium server
  Connection refused! Is selenium server started?
  POST /wd/hub/session - ECONNREFUSED 127.0.0.1:4444

Common causes

No Selenium server or driver running

The config points at port 4444, but the job never started a standalone Selenium or Grid hub there.

The driver started late or on another port

A managed driver (chromedriver/geckodriver) was not started, started late, or bound to a different port than the config expects.

How to fix it

Start the WebDriver server and wait for it

  1. Start Selenium standalone or the matching driver in CI.
  2. Poll until the WebDriver endpoint answers before running Nightwatch.
  3. Re-run so the new-session request succeeds.
.github/workflows/ci.yml
services:
  selenium:
    image: selenium/standalone-chrome:4.21.0
    ports: ['4444:4444']
- run: |
    for i in $(seq 1 30); do curl -sf http://localhost:4444/wd/hub/status && break || sleep 2; done
- run: npx nightwatch

Or let Nightwatch manage the local driver

Use the webdriver config so Nightwatch starts chromedriver/geckodriver itself instead of dialing a standalone server.

nightwatch.conf.js
// nightwatch.conf.js
module.exports = {
  webdriver: { start_process: true, server_path: require('chromedriver').path },
};

How to prevent it

  • Poll the WebDriver status endpoint before running Nightwatch.
  • Let Nightwatch start the driver locally when no Grid is needed.
  • Match the configured host and port to where the server listens.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →