fix: replace fs.promises.mkdtemp with mkdir for Workers compatibility#160
Open
ebrainte wants to merge 1 commit intocloudflare:mainfrom
Open
fix: replace fs.promises.mkdtemp with mkdir for Workers compatibility#160ebrainte wants to merge 1 commit intocloudflare:mainfrom
ebrainte wants to merge 1 commit intocloudflare:mainfrom
Conversation
Cloudflare Workers' nodejs_compat (via unenv) does not implement
fs.mkdtemp or fs.promises.mkdtemp. This causes a runtime crash when
using @cloudflare/playwright in a Worker environment.
Replace mkdtemp(prefix) with mkdir(prefix + randomSuffix, { recursive: true })
in the three call sites within browserType.ts and chromium.ts. The random
suffix uses Math.random().toString(36).slice(2), providing sufficient
uniqueness for temporary directory names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fs.promises.mkdtempwithfs.promises.mkdir+ random suffix in 3 call sites@cloudflare/playwrightin a Cloudflare WorkerProblem
Cloudflare Workers'
nodejs_compat(via unenv) does not implementfs.mkdtemporfs.promises.mkdtemp. When deploying the Playwright MCP example (or any Worker using@cloudflare/playwright), the Worker crashes at runtime with an error like:This affects
@cloudflare/playwright@1.1.2(and likely earlier versions).Fix
Replace
mkdtemp(prefix)withmkdir(prefix + randomSuffix, { recursive: true })in:packages/playwright-core/src/server/browserType.tspackages/playwright-core/src/server/browserType.tspackages/playwright-core/src/server/chromium/chromium.tsThe random suffix uses
Math.random().toString(36).slice(2), providing sufficient uniqueness for temporary directory names while using onlyfs.promises.mkdirwhich IS available in Workers.Requires
compatibility_date >= 2025-09-23in the Worker'swrangler.tomlforfs.promises.mkdirto be available.Testing
Verified by deploying the
cloudflare/playwright-mcpexample Worker with these changes patched viapatch-package. The Worker successfully launches browsers, navigates pages, and takes screenshots without anymkdtemp-related errors.