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.
Error retrieving a new session from the selenium server
Connection refused! Is selenium server started?
POST /wd/hub/session - ECONNREFUSED 127.0.0.1:4444Common 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
- Start Selenium standalone or the matching driver in CI.
- Poll until the WebDriver endpoint answers before running Nightwatch.
- Re-run so the new-session request succeeds.
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 nightwatchOr 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
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.