{"record":{"id":"3196416c8612b330","repo":"microsoft/playwright","slug":"fs-is-not-available-in-the-browser","errorCode":null,"errorMessage":"fs is not available in the browser","messagePattern":"fs is not available in the browser","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-client/src/nodeStubs/fs.ts","lineNumber":18,"sourceCode":"/**\n * Copyright (c) Microsoft Corporation.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nfunction notAvailable(): never {\n  throw new Error('fs is not available in the browser');\n}\n\nexport const promises: any = new Proxy({}, { get: () => notAvailable });\nexport const createReadStream: any = notAvailable;\nexport const createWriteStream: any = notAvailable;\n\nexport default { promises, createReadStream, createWriteStream };\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-client/src/nodeStubs/fs.ts#L1-L26","documentation":"Thrown by the browser-bundle stub of Node's `fs` module: any access to `fs`, `fs.promises.*`, `fs.createReadStream`, or `fs.createWriteStream` invokes `notAvailable()` which throws. The `promises` object is a Proxy whose every property getter throws, so even `fs.promises.readFile` triggers it.","triggerScenarios":"Code that imports/uses `fs` in a context resolved to `packages/playwright-client/src/nodeStubs/fs.ts` — i.e. the browser bundle of @playwright/client. Triggered by `import fs from 'fs'` then `fs.readFileSync(...)`, `fs.promises.readFile(...)`, or `fs.createReadStream(...)`.","commonSituations":"Pulling a Node-only utility into the browser client build; a dependency that lazily requires `fs`; bundler resolving `fs` to the stub instead of leaving it external; code shared between Node and browser that calls fs unconditionally.","solutions":["Gate fs usage behind an environment check (`import.meta.env?.SSR`, `typeof window`) and only call it in Node.","Move fs-dependent logic out of the browser bundle into a server/Node entry point.","Configure the bundler to externalize `fs` for the Node target instead of aliasing the stub.","Replace fs reads with the browser-friendly API (fetch, File API) where applicable."],"exampleFix":"// before\nimport fs from 'fs';\nconst cfg = fs.readFileSync('./config.json', 'utf8');  // throws in browser bundle\n\n// after\nlet cfg: string;\nif (typeof window === 'undefined') {\n  const fs = await import('fs');\n  cfg = fs.readFileSync('./config.json', 'utf8');\n} else {\n  const res = await fetch('/config.json');\n  cfg = await res.text();\n}","handlingStrategy":"type-guard","validationCode":"const isBrowserBundle = typeof window !== 'undefined' && (import.meta as any).env?.SSR === false;\nfunction safeReadFileSync(p: string): string {\n  if (isBrowserBundle) throw new Error('fs reads are disabled in the browser bundle');\n  return require('fs').readFileSync(p, 'utf8');\n}","typeGuard":"function fsAvailable(): boolean {\n  return typeof window === 'undefined' && typeof process !== 'undefined' && !!process.versions?.node;\n}","tryCatchPattern":"try {\n  return fs.readFileSync(p, 'utf8');\n} catch (e) {\n  if (/fs is not available in the browser/.test(e.message)) {\n    return await (await fetch(p)).text();\n  }\n  throw e;\n}","preventionTips":["Isolate fs imports in Node-only entry points.","Mark `fs` external in Node bundler targets.","Run a bundle analyzer to confirm the stub is not pulled into the browser build."],"tags":["browser-bundle","node-stubs","fs","environment"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}