{"record":{"id":"b277a36e068d155d","repo":"evanw/esbuild","slug":"the-write-option-is-unavailable-in-this-environm","errorCode":null,"errorMessage":"The \"write\" option is unavailable in this environment","messagePattern":"The \"write\" option is unavailable in this environment","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/shared/common.ts","lineNumber":927,"sourceCode":"  } catch (e) {\n    handleError(e, '')\n  }\n\n  // \"buildOrContext\" cannot be written using async/await due to \"buildSync\"\n  // and must be written in continuation-passing style instead\n  function buildOrContextContinue(requestPlugins: protocol.BuildPlugin[] | null, runOnEndCallbacks: RunOnEndCallbacks, scheduleOnDisposeCallbacks: () => void) {\n    const writeDefault = streamIn.hasFS\n    const {\n      entries,\n      flags,\n      write,\n      stdinContents,\n      stdinResolveDir,\n      absWorkingDir,\n      nodePaths,\n      mangleCache,\n    } = flagsForBuildOptions(callName, options, isTTY, buildLogLevelDefault, writeDefault)\n    if (write && !streamIn.hasFS) throw new Error(`The \"write\" option is unavailable in this environment`)\n\n    // Construct the request\n    const request: protocol.BuildRequest = {\n      command: 'build',\n      key: buildKey,\n      entries,\n      flags,\n      write,\n      stdinContents,\n      stdinResolveDir,\n      absWorkingDir: absWorkingDir || defaultWD,\n      nodePaths,\n      context: isContext,\n    }\n    if (requestPlugins) request.plugins = requestPlugins\n    if (mangleCache) request.mangleCache = mangleCache\n\n    // Factor out response handling so it can be reused for rebuilds","sourceCodeStart":909,"sourceCodeEnd":945,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/lib/shared/common.ts#L909-L945","documentation":"Thrown when the build option `write` is true but the esbuild service was started without filesystem access (i.e. `streamIn.hasFS` is false). The `write` option tells esbuild to write generated output files directly to the filesystem; that is impossible in environments such as the browser/WASM build where `hasFS` is set to `false` (lib/npm/browser.ts:142). By default `write` defaults to `true` only when the filesystem is available, so this only triggers when a caller explicitly passes `write: true` into the browser/WASM API.","triggerScenarios":"Calling esbuild's `build()`/`context()` from the browser or WASM package (`esbuild-wasm`) and passing `write: true` (or any truthy `write`) in the options, since `hasFS === false` there.","commonSituations":"Porting a Node-based esbuild script to the browser; using the WASM variant for in-browser bundling while reusing a config that sets `write: true`; default option resolution differing between the node and browser entry points.","solutions":["Set `write: false` in your build options and instead read the emitted files from the `outputFiles` array returned in the build result.","Switch to the native node package (`esbuild`) if you actually need filesystem writes, since `hasFS` is true there.","Detect the environment before passing `write` and only set it when running under Node."],"exampleFix":"// before (browser/wasm)\nawait esbuild.build({ entryPoints: ['app.ts'], bundle: true, write: true });\n// after\nlet result = await esbuild.build({ entryPoints: ['app.ts'], bundle: true, write: false });\nresult.outputFiles.forEach(f => console.log(f.path, f.text));","handlingStrategy":"validation","validationCode":"// Before calling build(), ensure write is compatible with the environment\nconst canWrite = typeof process !== 'undefined' && process.versions && process.versions.node;\nconst options = { ...opts };\nif (!canWrite) options.write = false;\nawait esbuild.build(options);","typeGuard":"function supportsFs(): boolean {\n  // esbuild sets hasFS true only in the node entry; approximate by node detection\n  return typeof process !== 'undefined' && !!process.versions && !!process.versions.node;\n}","tryCatchPattern":null,"preventionTips":["Never hardcode write: true in shared code that runs in the browser.","Read outputFiles from the result instead of relying on disk writes when targeting WASM."],"tags":["environment","filesystem","browser","wasm","build-options"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}