How to Start a Dev Server with Playwright’s webServer
If you put the start command and URL of a dev server in the webServer setting of your Playwright config file, npx playwright test alone handles the whole sequence: start the server, wait until it responds, run the tests, and stop the server. You no longer need to run npm run dev in another terminal before each E2E test run, and the same command works in CI.
The test environment was Ubuntu 24.04, Node.js 22.23.4, @playwright/test 1.63.0, and Vite 8.3.8. I used only Chromium (headless) as the browser. For servers other than Vite, I wrote a small Node.js HTTP server that lets me control startup delay and the port. I did not test running on a CI service, Windows, reusing a server with reuseExistingServer, or HTTPS servers.
- 1. Configuring Playwright to Start Vite with npm run dev
- 2. How Playwright Waits for the Server to Start
- 3. Point url at a Path That Responds When the Server Is Ready
- 4. Set cwd When the Config File Is in a Subfolder
- 5. Starting Two Servers: Frontend and API
- 6. How the Server Is Stopped After the Tests
- 7. Getting the Port Number from the Server's Output
Configuring Playwright to Start Vite with npm run dev
In a project where Vite is started by the dev script in package.json, I placed the following config file.
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests',
reporter: 'list',
use: {
baseURL: 'http://localhost:5173',
browserName: 'chromium',
},
webServer: {
command: 'npm run dev',
url: 'http://localhost:5173',
reuseExistingServer: !process.env.CI,
},
});
command is the command that starts the server, and url is where Playwright checks whether the server has finished starting: once this URL responds, startup is considered complete. If you set baseURL in use, you can write only the path in the tests, such as page.goto('/'). reuseExistingServer tells Playwright to use a server already running at that URL instead of starting one. The official webServer documentation recommends !process.env.CI, which starts a fresh server only on CI.
The test only checks that the count goes up when the button is pressed.
// tests/counter.spec.ts
import { test, expect } from '@playwright/test';
test('ボタンを押すと回数が増える', async ({ page }) => {
await page.goto('/');
const button = page.getByRole('button', { name: /押した回数/ });
await button.click();
await expect(button).toHaveText('押した回数:1');
});
When I ran it with no dev server running, Playwright started npm run dev and the test passed.
$ npx playwright test
Running 1 test using 1 worker
✓ 1 tests/counter.spec.ts:3:1 › ボタンを押すと回数が増える (197ms)
1 passed (1.8s)
I ran it three times in a row, and the total times were 1.8 s, 1.3 s, and 1.3 s. After the run finished, connecting to http://localhost:5173 with curl got no response, and no vite process was left running. Playwright stopped the server it had started when the tests ended.
How Playwright Waits for the Server to Start
You can see the waiting process by running with the environment variable DEBUG=pw:webserver. Here is part of the output for Vite (I omitted the first part of the timestamps).
02:47:13.254Z pw:webserver Process started
02:47:13.254Z pw:webserver Waiting for availability...
02:47:13.254Z pw:webserver HTTP GET: http://localhost:5173/
02:47:13.257Z pw:webserver Error while checking if http://localhost:5173/ is available: connect ECONNREFUSED 127.0.0.1:5173
02:47:13.257Z pw:webserver Waiting 100ms
02:47:13.357Z pw:webserver HTTP GET: http://localhost:5173/
…(中略)
[WebServer] VITE v8.3.8 ready in 199 ms
…(中略)
02:47:13.643Z pw:webserver HTTP Status: 200
02:47:13.645Z pw:webserver WebServer available
Playwright sends GET requests to url repeatedly and starts the tests once a response comes back. The intervals between checks grow: 100 ms, 250 ms, 500 ms, and after that, every 1 second. In the Playwright 1.63.0 source, the wait times are taken in order from [100, 250, 500], and once those are used up the wait is 1000 ms.
To see how this interval affects things, I prepared a server that takes 3 seconds to start listening and recorded the times of Playwright's checks in the same way.

The server began listening at about 3.0 seconds, but the check just before that was at 2.87 seconds, so Playwright did not notice until the next check at 3.87 seconds. Once the interval became 1 second, there can be a delay of up to about 1 second between the server being ready and the tests starting. Even so, in this setup where each test took about 0.1 seconds, the total time was 4.6 seconds.
The standard output of the command started by webServer is not shown by default (the default for stdout is 'ignore'). The Vite logs above appeared because I ran with DEBUG=pw:webserver. When I ran without it, nothing was shown.
Point url at a Path That Responds When the Server Is Ready
url does not just need to respond to anything. According to the TestConfig webServer documentation, startup is considered complete when the response is one of 2xx, 3xx, 400, 401, 402, or 403. 404 is not included.
If an API-only server has nothing at /, it returns 404. I made such a server wait with url: 'http://localhost:4000' and timeout: 5_000. It waited 5 seconds and then exited without starting the tests.
$ npx playwright test -c api-404.config.ts
Error: Timed out waiting 5000ms from config.webServer.
With DEBUG=pw:webserver, I could see that the server was running and kept returning 404 to the checks.
02:47:32.579Z pw:webserver HTTP Status: 404
02:47:32.582Z pw:webserver HTTP Status: 404
…(中略)
02:47:37.358Z pw:webserver HTTP Status: 404
Error: Timed out waiting 5000ms from config.webServer.
On the same server, I added a /health path that returns 200 and changed the setting to url: 'http://localhost:4000/health'. The test then passed in 0.7 seconds.
When I made a server that takes 8 seconds to start wait with timeout: 5_000, the message was exactly the same: Timed out waiting 5000ms from config.webServer. The message alone cannot tell you whether startup is slow or the server is running but the URL returns 404. When a timeout happens, run with DEBUG=pw:webserver and check whether HTTP Status lines appear. This helps you tell the two cases apart quickly. If startup is just slow, increase timeout (the default is 60 seconds).
Set cwd When the Config File Is in a Subfolder
By default, command runs in the folder that contains the config file (cwd in the TestConfig webServer documentation). I placed the config file in configs/ and kept command: 'node server.mjs'. When I ran it, Playwright looked for configs/server.mjs and failed. The output is partly omitted, and I replaced the path with /path/to/app.
$ npx playwright test -c configs/cwd.config.ts
[WebServer] Error: Cannot find module '/path/to/app/configs/server.mjs'
…(中略)
Error: Process from config.webServer was not able to start. Exit code: 1
In this case the server process exited right away, so the run stopped after 1.0 second without waiting for timeout (the default is 60 seconds). When I added cwd: '..' to webServer so the command ran from the project root, the test passed.
Starting Two Servers: Frontend and API
webServer can also take an array. This config starts an API server (listening after 2 seconds) and a frontend server (listening after 1 second).
// multi.config.ts (excerpt)
webServer: [
{
name: 'API',
command: 'NAME=api PORT=4000 DELAY=2000 node server.mjs',
url: 'http://localhost:4000/health',
stdout: 'pipe',
},
{
name: 'Web',
command: 'NAME=web PORT=4173 DELAY=1000 node server.mjs',
url: 'http://localhost:4173',
stdout: 'pipe',
},
],
With stdout: 'pipe', the server output is shown mixed into the test output. If you set name, the prefix of each line changes from the default [WebServer] to that name.
$ npx playwright test -c multi.config.ts
[API] [api] 起動を開始(2000ms 後に待ち受ける)PLAYWRIGHT_TEST=1
[API] [api] listening on port 4000(起動から 2010ms)
[API] [api] GET /health
[Web] [web] 起動を開始(1000ms 後に待ち受ける)PLAYWRIGHT_TEST=1
[Web] [web] listening on port 4173(起動から 1009ms)
[Web] [web] GET /
Running 1 test using 1 worker
[Web] [web] GET /
[API] [api] GET /api/message
✓ 1 tests/multi.spec.ts:3:1 › 画面と API の両方に届く (160ms)
1 passed (5.6s)
PLAYWRIGHT_TEST=1 in the output is the server printing its own environment variable. As the official webServer documentation says, PLAYWRIGHT_TEST=1 was passed to the commands started by webServer.
The order of startup is worth noting. The official documentation says that multiple servers can be started "at the same time." In this test environment (1.63.0), however, the frontend server started only after the API server had finished starting and /health had responded. In the DEBUG=pw:webserver output, the second Starting WebServer process appears 2 milliseconds after the first WebServer available.
02:47:55.725Z pw:webserver Starting WebServer process NAME=api PORT=4000 DELAY=2000 node server.mjs...
02:47:58.604Z pw:webserver WebServer available
02:47:58.606Z pw:webserver Starting WebServer process NAME=web PORT=4173 DELAY=1000 node server.mjs...
02:48:00.476Z pw:webserver WebServer available
So the two waits add up, and the whole run took 5.6 seconds. If you list servers that take a long time to start, you need to allow for the total of these waits.
How the Server Is Stopped After the Tests
According to the TestConfig webServer documentation, if you specify nothing, the process group that was started is forcibly stopped with SIGKILL. I added code to the test server so that when it receives SIGTERM, it writes one line to shutdown.log and then exits. When I ran with the default settings, the test passed, but shutdown.log was not created. The server was stopped without running the code that handles SIGTERM.
If you write gracefulShutdown, Playwright sends the signal you specify first, and if the process has not exited after the number of milliseconds given by timeout, it sends SIGKILL.
webServer: {
command: 'node server.mjs',
url: 'http://localhost:4173',
stdout: 'pipe',
gracefulShutdown: { signal: 'SIGTERM', timeout: 5_000 },
},
With this setting, [WebServer] [web] SIGTERM を受け取った appeared after the tests, and shutdown.log contained the single line "received SIGTERM, cleaned up, and exiting." After stopping with the default settings, I could still start a server on port 4173 right away. After stopping with gracefulShutdown, connecting to port 4173 got no response.
If your server does cleanup when it exits, such as writing files or deleting temporary files, that cleanup does not run with the default SIGKILL. Set gracefulShutdown. Also, according to the TestConfig documentation, gracefulShutdown is not used on Windows.
Getting the Port Number from the Server's Output
If you use wait instead of url, startup is considered complete once a fixed string appears in the server's output. If you write a named group in the regular expression, that part is stored in an environment variable (TestConfig webServer documentation). Using this, I started a server on a free port and ran the test against that port.
// wait.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests',
testMatch: 'port.spec.ts',
reporter: 'list',
use: { baseURL: `http://localhost:${process.env.WEB_PORT}` },
webServer: {
command: 'PORT=0 node server.mjs',
stdout: 'pipe',
wait: { stdout: /listening on port (?<web_port>\d+)/ },
},
});
When started with PORT=0, the test server listens on a free port chosen by the OS and prints something like listening on port 44309. The group name web_port becomes the environment variable WEB_PORT in uppercase. I ran it twice, and the ports were 44309 and 40015. In both runs, page.goto('/') in the test opened the port and passed.
$ npx playwright test -c wait.config.ts
[WebServer] [web] 起動を開始(0ms 後に待ち受ける)PLAYWRIGHT_TEST=1
[WebServer] [web] listening on port 44309(起動から 10ms)
Running 1 test using 1 worker
[WebServer] [web] GET /
開いた URL: http://localhost:44309/
✓ 1 tests/port.spec.ts:3:1 › ランダムなポートのサーバーを開く (126ms)
1 passed (988ms)
In this test, reading process.env.WEB_PORT in the config file's baseURL worked both times. However, the official documentation only says that named groups are stored in environment variables. It does not describe using them in baseURL this way. This approach works when you do not want to fix the port number, but after you update Playwright, check first that this setup still works.





