microsoft/playwright · error · Error
Cannot write to a directory
Error message
Cannot write to a directory
What it means
WritableStreamDispatcher can wrap either an fs.WriteStream (a real writable file) or a plain string representing a directory path (used internally when the target is a directory marker). The write() method throws when streamOrDirectory is a string, because bytes cannot be appended to a directory. This guard catches misuse of the protocol-level stream API.
Source
Thrown at packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts:46
readonly lastModifiedMs: number | undefined;
constructor(parent: SdkObject, streamOrDirectory: fs.WriteStream | string, lastModifiedMs: number | undefined) {
super(parent, 'stream');
this.streamOrDirectory = streamOrDirectory;
this.lastModifiedMs = lastModifiedMs;
}
}
export class WritableStreamDispatcher extends Dispatcher<WritableStreamSdkObject, channels.WritableStreamChannel, BrowserContextDispatcher> implements channels.WritableStreamChannel {
_type_WritableStream = true;
constructor(scope: BrowserContextDispatcher, streamOrDirectory: fs.WriteStream | string, lastModifiedMs?: number) {
super(scope, new WritableStreamSdkObject(scope._object, streamOrDirectory, lastModifiedMs), 'WritableStream', {});
}
async write(params: channels.WritableStreamWriteParams, progress: Progress): Promise<channels.WritableStreamWriteResult> {
if (typeof this._object.streamOrDirectory === 'string')
throw new Error('Cannot write to a directory');
const stream = this._object.streamOrDirectory;
await progress.race(new Promise<void>((fulfill, reject) => {
stream.write(params.binary, error => {
if (error)
reject(error);
else
fulfill();
});
}));
}
async close(params: channels.WritableStreamCloseParams, progress: Progress): Promise<void> {
if (typeof this._object.streamOrDirectory === 'string')
throw new Error('Cannot close a directory');
const stream = this._object.streamOrDirectory;
await progress.race(new Promise<void>(fulfill => stream.end(fulfill)));
if (this._object.lastModifiedMs)
await progress.race(fs.promises.utimes(this.path(), new Date(this._object.lastModifiedMs), new Date(this._object.lastModifiedMs)));View on GitHub (pinned to c8fc3bf8d3)
Solutions
- Pass a concrete file path, not a directory, when creating a writable stream / saveAs target.
- Construct the destination with path.join(dir, 'file.ext') so it always resolves to a file.
- Verify the path with fs.statSync(path).isFile() before writing.
Example fix
// before
await download.saveAs('/tmp/downloads/'); // directory → write throws
// after
await download.saveAs('/tmp/downloads/report.pdf'); // file path Defensive patterns
Strategy: validation
Validate before calling
// Ensure the save target is a file path, not a directory.
const dest = path.join(downloadDir, suggestedName);
if (fs.statSync(dest).isDirectory()) throw new Error('target is a directory');
await download.saveAs(dest); Type guard
// Confirm the path resolves to a file before writing.
function isFilePath(p: string): boolean {
try { return fs.statSync(p).isFile(); } catch { return false; }
} Prevention
- Always pass a full file path (path.join(dir, name)) to saveAs, never a bare directory.
- Verify the destination with fs.statSync(...).isFile() before writing.
- Treat directory-string stream wrappers as read-only markers.
When it happens
Trigger: Constructing a WritableStreamDispatcher with a directory string (as saveAs does to mark a directory upload target) and then calling write() on it; passing a directory path where a file write stream was expected through the saveAs / saveStorageState path.
Common situations: This is primarily an internal/protocol-level error surfaced when a save target resolves to a directory rather than a file — e.g. a download or HAR saveAs path that points at an existing directory; rarely hit directly by end-user code.
Related errors
- Cannot close a directory
- Path is not available when connecting remotely. Use saveAs()
- ${path}: expected channel ${names.toString()}
- Cannot find command to respond: ${id}
- Cannot find object to "${method}": ${guid}
AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12).
Data as JSON: /api/errors/cf83d1f995339e52.
Report an issue: GitHub.