Implementations of the node:fs module and the Web File System API are now available in Workers.
Using the node:fs module
The node:fs module provides access to a virtual file system in Workers. You can use it to read and write files, create directories, and perform other file system operations.
The virtual file system is ephemeral with each individual request havig its own isolated temporary file space. Files written to the file system will not persist across requests and will not be shared across requests or across different Workers.
Workers running with the nodejs_compat compatibility flag will have access to the node:fs module by default when the compatibility date is set to 2025-09-01 or later. Support for the API can also be enabled using the enable_nodejs_fs_module compatibility flag together with the nodejs_compat flag. The node:fs module can be disabled using the disable_nodejs_fs_module compatibility flag.
import fs from "node:fs";
const config = JSON.parse(fs.readFileSync("/bundle/config.json", "utf-8"));
export default { async fetch(request) { return new Response(`Config value: ${config.value}`); },};There are a number of initial limitations to the node:fs implementation:
- The glob APIs (e.g.
fs.globSync(...)) are not implemented. - The file watching APIs (e.g.
fs.watch(...)) are not implemented. - The file timestamps (modified time, access time, etc) are only partially supported. For now, these will always return the Unix epoch.
Refer to the Node.js documentation for more information on the node:fs module and its APIs.
The Web File System API
The Web File System API provides access to the same virtual file system as the node:fs module, but with a different API surface. The Web File System API is only available in Workers running with the enable_web_file_system compatibility flag. The nodejs_compat compatibility flag is not required to use the Web File System API.
const root = navigator.storage.getDirectory();
export default { async fetch(request) { const tmp = await root.getDirectoryHandle("/tmp"); const file = await tmp.getFileHandle("data.txt", { create: true }); const writable = await file.createWritable(); const writer = writable.getWriter(); await writer.write("Hello, World!"); await writer.close();
return new Response("File written successfully!"); },};As there are still some parts of the Web File System API tht are not fully standardized, there may be some differences between the Workers implementation and the implementations in browsers.
Source: Cloudflare
Latest Posts
- Microsoft Defender for Office 365: Admins can block external users in Microsoft Teams from Defender Portal [MC1200058]
![Microsoft Defender for Office 365: Admins can block external users in Microsoft Teams from Defender Portal [MC1200058] 2 pexels mirrographer 1194036](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)
- Microsoft Purview: Role management update [MC1199765]
![Microsoft Purview: Role management update [MC1199765] 3 pexels googledeepmind 17483871](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)
- Outlook: New and improved People Hub on Web [MC1199768]
![Outlook: New and improved People Hub on Web [MC1199768] 4 pexels therato 3408744](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)
- Learning Activities now available in Microsoft 365 Education and Copilot [MC1199766]
![Learning Activities now available in Microsoft 365 Education and Copilot [MC1199766] 5 pexels noellegracephotos 906018](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==)

![Microsoft Defender for Office 365: Admins can block external users in Microsoft Teams from Defender Portal [MC1200058] 2 pexels mirrographer 1194036](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-mirrographer-1194036-150x150.webp)
![Microsoft Purview: Role management update [MC1199765] 3 pexels googledeepmind 17483871](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-googledeepmind-17483871-150x150.webp)
![Outlook: New and improved People Hub on Web [MC1199768] 4 pexels therato 3408744](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-therato-3408744-150x150.webp)
![Learning Activities now available in Microsoft 365 Education and Copilot [MC1199766] 5 pexels noellegracephotos 906018](https://mwpro.co.uk/wp-content/uploads/2024/08/pexels-noellegracephotos-906018-150x150.webp)
