electric-sql / electric-sql/pglite

`pglite-server --run` fails to execute batch files (.cmd) on Windows with `spawn ENOENT`

Open
#776 1 comment 2 reactions 1 assignee Claimed by @tdrz View on GitHub
Dominant language
TypeScript
Stars
16k
Forks
442
Avg merge
20h 19m
Merged PRs (30d)
7

Description

## Describe the bug

When using the `pglite-server --run "..."` command on a Windows environment, the subprocess fails to launch if the command is a batch file (e.g., `.cmd` or `.bat`). This commonly affects widely used tools like `npm` and `npx`, which are executed via `npm.cmd` on Windows.

The command fails with an `Error: spawn ENOENT`, even when the executable is correctly located in the system's `PATH`.

Further investigation revealed that commands pointing to binary executables (e.g., `node.exe`) run successfully, while commands that rely on batch files do not. This strongly suggests that the underlying `child_process.spawn` call within `pglite-server` is being made without the necessary shell context on Windows.

On Windows, `.cmd` files are not directly executable by the OS; they must be interpreted by the command shell (`cmd.exe`). The default behavior of Node.js's `spawn` function does not use a shell, leading it to search for a non-existent `.exe` and resulting in the `ENOENT` error.

## To Reproduce

Steps to reproduce the behavior:

1. **Environment:** Windows OS with Node.js and npm installed.
2. **`package.json` setup:**
```json
{
"name": "pglite-test",
"devDependencies": {
"@electric-sql/pglite": "^0.3.7",
"@electric-sql/pglite-socket": "^0.0.12"
}
}
```
3. **Run command:**
```shell
npx pglite-server --run "npm -v"
```
4. **Observe the error:**
```
Error running command: Error: spawn npm ENOENT
at ChildProcess._handle.onexit (node:internal/child_process:286:19)
at onErrorNT (node:internal/child_process:484:16)
at process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
errno: -4058,
code: 'ENOENT',
syscall: 'spawn npm',
path: 'npm',
spawnargs: [ '-v' ]
}
Subprocess failed to start, shutting down...

Shutting down PGLiteSocketServer...
Terminating child process...
Command exited with code -4058
Child process failed with exit code -4058, shutting down...

Shutting down PGLiteSocketServer...
Server stopped
```

## Expected behavior

The `pglite-server` should successfully execute the nested `npm -v` command within a subprocess on Windows, mirroring its behavior on Unix-like systems.

## Proposed Solutions

This is a common cross-platform challenge in Node.js development. Here are a few potential solutions to make `pglite-server` more robust on Windows:

1. Use `cross-spawn`
The most idiomatic and reliable solution is to replace the native `child_process.spawn` with the [`cross-spawn`](https://www.google.com/search?q=%5Bhttps://github.com/moxystudio/cross-spawn%5D\(https://github.com/moxystudio/cross-spawn\)) library. It is a drop-in replacement that transparently handles the complexities of spawning processes on Windows, including the automatic use of a shell for `.cmd` and `.bat` files. This would resolve the issue without requiring platform-specific code.

2. Conditionally Use the `shell: true` Option
An alternative is to modify the existing `spawn` call to include the `shell: true` option when running on Windows. This forces the command to be executed within the default system shell (`cmd.exe`), which can correctly interpret batch files.

Thank you for this excellent tool. I hope this report is helpful in improving its cross-platform compatibility.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.