|
| 1 | +--- |
| 2 | +pcx_content_type: configuration |
| 3 | +title: fs |
| 4 | +--- |
| 5 | + |
| 6 | +import { Render, TypeScriptExample } from "~/components"; |
| 7 | + |
| 8 | +<Render file="nodejs-compat-howto" /> |
| 9 | + |
| 10 | +You can use [`node:fs`](https://nodejs.org/api/fs.html) to access a virtual file |
| 11 | +system in Workers. |
| 12 | + |
| 13 | +<TypeScriptExample filename="index.ts"> |
| 14 | +```ts |
| 15 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 16 | + |
| 17 | +const config = readFileSync('/bundle/config.txt', 'utf8'); |
| 18 | + |
| 19 | +writeFileSync('/tmp/abc.txt', 'Hello, world!'); |
| 20 | +``` |
| 21 | +</TypeScriptExample> |
| 22 | + |
| 23 | +The Workers Virtual File System (VFS) is a memory-based file system that allows |
| 24 | +you to read modules included in your Worker bundle as read-only files, access a |
| 25 | +temporary directory for writing temporary files, or access common character |
| 26 | +devices like `/dev/null`, `/dev/random`, `/dev/full`, and `/dev/zero`. |
| 27 | + |
| 28 | +The directory structure initially looks like: |
| 29 | + |
| 30 | +``` |
| 31 | +/bundle |
| 32 | + └── (one file for each module in your Worker bundle) |
| 33 | +/tmp |
| 34 | + └── (empty, but you can write files, create directories, symlinks, etc) |
| 35 | +/dev |
| 36 | + ├── null |
| 37 | + ├── random |
| 38 | + ├── full |
| 39 | + └── zero |
| 40 | +``` |
| 41 | + |
| 42 | +The `/bundle` directory contains the files for all modules included in your |
| 43 | +Worker bundle, which you can read using APIs like `readFileSync` or |
| 44 | +`read(...)`, etc. These are always read-only. |
| 45 | + |
| 46 | +The `/tmp` directory is writable, and you can use it to create temporary files |
| 47 | +or directories. You can also create symlinks in this directory. However, the |
| 48 | +contents of `/tmp` are not persistent and are unique to each request. This means |
| 49 | +that files created in `/tmp` within the context of one request will not be |
| 50 | +available in other concurrent or subsequent requests. |
| 51 | + |
| 52 | +The `/dev` directory contains common character devices: |
| 53 | + |
| 54 | +* `/dev/null`: A null device that discards all data written to it and returns |
| 55 | + EOF on read. |
| 56 | +* `/dev/random`: A device that provides random bytes on reads and discards all |
| 57 | + data written to it. Reading from `/dev/random` is only permitted when within |
| 58 | + the context of a request. |
| 59 | +* `/dev/full`: A device that always returns EOF on reads and discards all data |
| 60 | + written to it. |
| 61 | +* `/dev/zero`: A device that provides an infinite stream of zero bytes on reads |
| 62 | + and discards all data written to it. |
| 63 | + |
| 64 | +All operations on the VFS are synchronous. You can use the synchronous, |
| 65 | +asynchronous callback, or promise-based APIs provided by the `node:fs` module |
| 66 | +but all operations will be performed synchronously. |
| 67 | + |
| 68 | +Timestamps for files in the VFS are currently always set to the Unix epoch |
| 69 | +(`1970-01-01T00:00:00Z`). This means that operations that rely on timestamps, |
| 70 | +like `fs.stat`, will always return the same timestamp for all files in the VFS. |
| 71 | +This is a temporary limitation that will be addressed in a future release. |
| 72 | + |
| 73 | +Since all temporary files are held in memory, the total size of all temporary |
| 74 | +files created count towards your Worker’s memory limit. If you exceed this |
| 75 | +limit, the Worker instance will be terminated and restarted. |
| 76 | + |
| 77 | +::: |
| 78 | + |
| 79 | +The full `node:fs` API is documented in the [Node.js documentation for `node:fs`](https://nodejs.org/api/fs.html). |
0 commit comments